如何将数组项放在php的末尾?
我想将某些数组项放到最后一个位置,当我循环并将$ arr输出到html时,这将始终将“其他”项保留在末尾。最佳和简便的方法是什么?
<?php
$arr=array(
'a'=>'hello',
'game'=>'boy',
'other'=>'good',
'name'=>'jimmy',
//...
);
// how to resort $arr to put other item to then end of $arr
$arr=array(
'a'=>'hello',
'game'=>'boy',
'name'=>'jimmy',
//...
'other'=>'good'
);
?>
假设您不是简单地要求给定数组,则根据键的字母属性对数组进行排序:
$arr=array(
'a'=>'hello',
'game'=>'boy',
'other'=>'good',
'name'=>'jimmy',
);
您必须先取出旧密钥,然后保存其值:
$old = $arr['other'];
unset($arr['other']);
然后,将其附加到数组中,如下所示:
$arr += array('other' => $old);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句