PHP:将关联数组元素移动到数组的开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11276313/
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
PHP: Move associative array element to beginning of array
提问by Mark Eirich
What would be the best method of moving any element of an associative array to the beginning of the array?
将关联数组的任何元素移动到数组开头的最佳方法是什么?
For example, say I have the following array:
例如,假设我有以下数组:
$myArray = array(
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'one' => 'Blah Blah Blah 1',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
);
What i want to do is move the 'one' element to the beginning and end up with the following array:
我想要做的是将“一个”元素移动到开头并以以下数组结尾:
$myArray = array(
'one' => 'Blah Blah Blah 1',
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
);
回答by Mark Eirich
You can use the array union operator (+) to join the original array to a new associative array using the known key (one).
您可以使用数组联合运算符 ( +) 将原始数组连接到使用已知键 ( one)的新关联数组。
$myArray = array('one' => $myArray['one']) + $myArray;
// or ['one' => $myArray['one']] + $myArray;
Array keys are unique, so it would be impossible for it to exist in two locations.
数组键是唯一的,因此它不可能存在于两个位置。
Please see php.net/manual/en/language.operators.array.php:
请参阅 php.net/manual/en/language.operators.array.php:
The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.
+ 运算符返回附加到左侧数组的右侧数组;对于存在于两个数组中的键,将使用左侧数组中的元素,而忽略右侧数组中的匹配元素。
回答by trante
If you have numerical array keys and want to reindex array keys, it would be better to put it into array_mergelike this:
如果您有数字数组键并想重新索引数组键,最好将其放入array_merge:
$myArray = array_merge(array($key => $value) + $myArray );
回答by Sune
A bit late, but in case anyone needs it, I created this little snippet.
有点晚了,但如果有人需要它,我创建了这个小片段。
function arr_push_pos($key, $value, $pos, $arr)
{
$new_arr = array();
$i = 1;
foreach ($arr as $arr_key => $arr_value)
{
if($i == $pos)
$new_arr[$key] = $value;
$new_arr[$arr_key] = $arr_value;
++$i;
}
return $new_arr;
}
Just adjust it to suit your needs, or use it and unset the index to move. Works with associative arrays too.
只需调整它以满足您的需要,或者使用它并取消设置索引以移动。也适用于关联数组。
回答by billynoah
Here's another simple one-liner that gets this done using array_splice():
这是另一个简单的单行代码,它使用以下方法完成array_splice():
$myArray = array_splice($myArray,array_search('one',array_keys($myArray)),1) + $myArray;
回答by Eugene Kaurov
if you have 2 arrays, 1st has elements to move to the top of 2nd array of elements, you can use
如果你有 2 个数组,第一个有元素移动到第二个元素数组的顶部,你可以使用
$result = \array_replace($ArrayToMoveToTop, $myArray);
Here is a code sample:
这是一个代码示例:
//source array
$myArray = [
'two' => 'Blah Blah Blah 2',
'three' => 'Blah Blah Blah 3',
'one' => 'Blah Blah Blah 1',
'four' => 'Blah Blah Blah 4',
'five' => 'Blah Blah Blah 5',
];
// set necessary order
$orderArray = [
'one' => '',
'two' => '',
];
//apply it
$result = \array_replace($orderArray, $myArray);
\print_r($result);
回答by Emil Vikstr?m
There's a function in the comments of the PHP manual for array_unshiftwhich can be used to add an element, with key, to the beginning of an array:
PHP 手册的array_unshift注释中有一个函数,可用于将带有键的元素添加到数组的开头:
function array_unshift_assoc(&$arr, $key, $val)
{
$arr = array_reverse($arr, true);
$arr[$key] = $val;
return array_reverse($arr, true);
}
Unset the element and reinsert it again with the above function:
取消设置元素并使用上述函数重新插入它:
$tmp = $myArray['one'];
unset($myArray['one']);
$myArray = array_unshift_assoc($myArray, 'one', $tmp);
A more general approach may be to use uksortto sort your array by keys and provide a sorting function of your own.
更通用的方法可能是使用uksort按键对数组进行排序并提供您自己的排序功能。

