如何在php关联数组中进行反向迭代?https://dev59.com/o2gv5IYBdhLWcg3wMN-0#10777617 提供了非关联数组的解决方案。
我的尝试:
产生以下结果:
我希望保留这些密钥并返回它们。
我的尝试:
$a=['5'=>'five','3'=>'three','7'=>'seven'];
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
$a=array_reverse($a);
var_dump($a);
foreach($a as $k=>$v){echo("$k $v\n");}
产生以下结果:
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[0]=>
string(5) "seven"
[1]=>
string(5) "three"
[2]=>
string(4) "five"
}
0 seven
1 three
2 five
我希望保留这些密钥并返回它们。
array(3) {
[5]=>
string(4) "five"
[3]=>
string(5) "three"
[7]=>
string(5) "seven"
}
5 five
3 three
7 seven
array(3) {
[7]=>
string(5) "seven"
[3]=>
string(5) "three"
[5]=>
string(4) "five"
}
7 seven
3 three
5 five