PHP-将项目添加到关联数组的开头 [英] PHP - add item to beginning of associative array
本文介绍了PHP-将项目添加到关联数组的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将一个项目添加到关联数组的开头?例如,假设我有一个像这样的数组:
How can I add an item to the beginning of an associative array? For example, say I have an array like this:
$arr = array('key1' => 'value1', 'key2' => 'value2');
当我在$arr['key0'] = 'value0';
中添加一些内容时,我得到:
When I add something to it as in $arr['key0'] = 'value0';
, I get:
Array
(
[key1] => value1
[key2] => value2
[key0] => value0
)
我该如何做到
Array
(
[key0] => value0
[key1] => value1
[key2] => value2
)
谢谢,
三通
推荐答案
您可以使用 union 运算符:
You could use the union operator:
$arr1 = array('key0' => 'value0') + $arr1;
或 array_merge
.
这篇关于PHP-将项目添加到关联数组的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文