在 PHP 中将数组元素移动到顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5312879/
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
Moving array element to top in PHP
提问by Val
$arr = array(
'a1'=>'1',
'a2'=>'2'
);
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 :)
我需要将 a2 移到顶部,并将其a2
作为关键我将如何继续我似乎无法想出一种方法而不会将某些事情搞砸:)
采纳答案by Michiel Pater
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 )
*/
回答by amartynov
Here is a solution which works correctly both with numeric and string keys:
这是一个适用于数字和字符串键的解决方案:
function move_to_top(&$array, $key) {
$temp = array($key => $array[$key]);
unset($array[$key]);
$array = $temp + $array;
}
It works because arrays in PHP are ordered maps.
Btw, to move an item to bottom use:
它之所以有效是因为 PHP 中的数组是有序映射。
顺便说一句,要将项目移动到底部,请使用:
function move_to_bottom(&$array, $key) {
$value = $array[$key];
unset($array[$key]);
$array[$key] = $value;
}
回答by billynoah
Here's a simple one liner to get this done with array_splice()
and the union
operator:
这是一个简单的单层衬里array_splice()
和union
操作员来完成这项工作:
$arr = array('a1'=>'1', 'a2'=>'2', 'a3' => '3');
$arr = array_splice($arr,array_search('a2',array_keys($arr)),1) + $arr;
Edit:
编辑:
In retrospect I'm not sure why I wouldn't just do this:
回想起来,我不确定为什么我不这样做:
$arr = array('a2' => $arr['a2']) + $arr;
Cleaner, easier and probably faster.
更干净、更容易,而且可能更快。
回答by jalmatari
try this:
尝试这个:
$key = 'a3';
$arr = [
'a1' => '1',
'a2' => '2',
'a3' => '3',
'a4' => '4',
'a5' => '5',
'a6' => '6'
];
if (isset($arr[ $key ]))
$arr = [ $key => $arr[ $key ] ] + $arr;
result:
结果:
array(
'a3' => '3',
'a1' => '1',
'a2' => '2',
'a4' => '4',
'a5' => '5',
'a6' => '6'
)
回答by Scott
You can also look at array_multisortThis lets you use one array to sort another. This could, for example, allow you to externalize a hard-coded ordering of values into a config file.
您还可以查看array_multisort这使您可以使用一个数组对另一个数组进行排序。例如,这可以允许您将值的硬编码排序外部化到配置文件中。
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
在这个例子中,排序后,第一个数组将包含 0、10、100、100。第二个数组将包含 4、1、2、3。第二个数组中的条目对应于第一个数组中的相同条目(100和 100) 也被排序。
Outputs:
输出:
array(4) {
[0]=> int(0)
[1]=> int(10)
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(4)
[1]=> int(1)
[2]=> int(2)
[3]=> int(3)
}