如何在 PHP 中将具有已知键的数组元素移动到数组的末尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2359652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:10:33  来源:igfitidea点击:

How do I move an array element with a known key to the end of an array in PHP?

phparrayssorting

提问by tamewhale

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.

我能想到的每一种方式似乎都有点笨拙,我觉得我错过了一些明显的东西。

回答by cletus

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;

回答by Gordon

array_shiftis probably less efficient than unsetting the index, but it works:

array_shift可能比取消设置 index效率低,但它有效:

$my_array = array('monkey' => 1, 'giraffe' => 2, 'lion' => 3);
$my_array['monkey'] = array_shift($my_array);
print_r($my_array);

Another alternative is with a callback and uksort:

另一种选择是使用回调和uksort

uksort($my_array, create_function('$x,$y','return ($y === "monkey") ? -1 : 1;'));

You will want to use a proper lambdaif you are using PHP5.3+ or just define the function as a global function regularly.

如果您使用 PHP5.3+ 或只是定期将函数定义为全局函数,您将需要使用适当的lambda

回答by billynoah

I really like @Gordon's answer above for it's elegance as a one liner, but it only works if the key is at the beginning. Here's another one liner that will work for a key in any position:

我真的很喜欢上面@Gordon 的回答,因为它作为单衬很优雅,但只有在关键在开头时才有效。这是另一个适用于任何位置的钥匙的衬垫:

$arr = array('monkey' => 1, 'giraffe' => 2, 'lion' => 3);
$arr += array_splice($arr,array_search('giraffe',array_keys($arr)),1);

EDIT:Beware, this fails with numeric keys.

编辑:当心,这会因数字键而失败。

回答by Alexander Dolgopolskiy

Contributing to the accepted reply - for the element not to be inserted into the same position, but to be placed at the end of the array:

有助于接受的回复 - 元素不会被插入到相同的位置,而是被放置在数组的末尾:

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

instead of:

代替:

$my_array['monkey'] = $v;

use:

用:

array_push($my_array, $v);

回答by Andrea

You can implement some basic calculus and get a universal function for moving array element from one position to the other.

您可以实现一些基本的微积分,并获得将数组元素从一个位置移动到另一个位置的通用函数。

For PHP it looks like this:

对于 PHP,它看起来像这样:

function magicFunction ($targetArray, $indexFrom, $indexTo) { 
    $targetElement = $targetArray[$indexFrom]; 
    $magicIncrement = ($indexTo - $indexFrom) / abs ($indexTo - $indexFrom); 

    for ($Element = $indexFrom; $Element != $indexTo; $Element += $magicIncrement){ 
        $targetArray[$Element] = $targetArray[$Element + $magicIncrement]; 
    } 

    $targetArray[$indexTo] = $targetElement; 
}

Check out "moving array elements" at "gloommatter" for detailed explanation.

查看“gloommatter”中的“移动数组元素”以获取详细说明。

http://www.gloommatter.com/DDesign/programming/moving-any-array-elements-universal-function.html

http://www.gloommatter.com/DDesign/programming/moving-any-array-elements-universal-function.html