怎样用已知键在PHP中数组的结尾移动数组元素? [英] How do I move an array element with a known key to the end of an array in PHP?

查看:133
本文介绍了怎样用已知键在PHP中数组的结尾移动数组元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有过相当琐碎的问题,大脑冻结。如果我开始一个这样的数组:

Having a brain freeze over a fairly trivial problem. If I start with an array like this:

$my_array = array(
                  'monkey'  => array(...),
                  'giraffe' => array(...),
                  'lion'    => array(...)
);

...新的元素可能会添加不同的密钥,但总是一个数组值。现在,我可以肯定的第一个元素总是要有钥匙'猴子',但我不能确定的其他任何按键。

...and new elements might get added with different keys but always an array value. Now I can be sure the first element is always going to have the key 'monkey' but I can't be sure of any of the other keys.

当我填完我想移动的元素称为猴子到数组的末尾,而不会干扰其他元素的顺序数组。什么是做到这一点的最有效方法是什么?

When I've finished filling the array I want to move the known element 'monkey' to the end of the array without disturbing the order of the other elements. What is the most efficient way to do this?

每一个方式,我能想到的似乎有点笨重,我觉得我失去了一些东西明显。

Every way I can think of seems a bit clunky and I feel like I'm missing something obvious.

推荐答案

我能想到的唯一办法做到这一点是将其删除然后​​将其添加:

The only way I can think to do this is to remove it then add it:

$v = $my_array['monkey'];
unset($my_array['monkey']);
$my_array['monkey'] = $v;

这篇关于怎样用已知键在PHP中数组的结尾移动数组元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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