将特定项目的数组放在它的末尾
问题描述:
我想选择一个PHP数组的特定项目并将其放在数组的末尾。我的数组包含未知数量的项目(也就是说,您事先不知道会有多少项目),我想选择一个带有密钥Other
的项目并将其放在数组的末尾。将特定项目的数组放在它的末尾
我试了一些array_diff()
,但我无法设法选择Other
项目。我可以用foreach循环中的Other
-key选择和取消设置,但不能将它放在数组的末尾。所以任何建议都会很棒。
答
$arr = array('key' => 'test', 'other' => 'test2', 'key2' => 'test3');
$arr_other = $arr['other'];
unset($arr['other']);
$arr['other'] = $arr_other;
print_r($arr);
+0
就是这么简单,但我没有看到它。感谢您的回答! – Michiel 2012-04-16 07:18:17
答
$tmp = $array['Other'];
unset($array['Other']);
$array['Other'] = $tmp;
答
$array = array(
'one' => 'some value',
'other' => 'some value',
'two' => 'some value',
'three' => 'some value',
);
$other = $array['other'];
unset($array['other']);
$array['other'] = $other;
你可以张贴样品阵列? – 2012-04-16 07:04:57