在 PHP 中将数组元素移动到顶部 [英] Moving array element to top in PHP
本文介绍了在 PHP 中将数组元素移动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$arr = array(
'a1'=>'1',
'a2'=>'2'
);
我需要将 a2 移到顶部,并将 a2
作为关键我将如何继续我似乎无法想出一种方法而不会将某些事情搞砸:)
I need to move the a2 to the top, as well as keep the a2
as a key how would I go on about it I can't seem to think a way without messing something up :)
推荐答案
你可以这样实现:
$arr = array(
'a1'=>'1',
'a2'=>'2'
);
end($arr);
$last_key = key($arr);
$last_value = array_pop($arr);
$arr = array_merge(array($last_key => $last_value), $arr);
/*
print_r($arr);
will output (this is tested):
Array ( [a2] => 2 [a1] => 1 )
*/
这篇关于在 PHP 中将数组元素移动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文