移动数组元素在PHP顶部 [英] Moving array element to top in PHP

查看:125
本文介绍了移动数组元素在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 :)

推荐答案

您可以做到这一点是这样的:

You can achieve that this way:

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆