Php 如何使用键在数组的开头添加值?

Php 如何使用键在数组的开头添加值?,php,Php,我知道函数array\u unshift()。 对于此代码: $messages[$obj_result['from']] = $obj_result; 我需要在数组的开头添加value$obj_result。因此,最后一个附加值将位于数组的开头。假设作为一个数组(使用您的desire键),您可以使用operator+: $messages = obj_result + $messages; 从手册中: while literal keys won't be touched 因此,您只需在

我知道函数
array\u unshift()。
对于此代码:

$messages[$obj_result['from']] = $obj_result;
我需要在数组的开头添加value
$obj_result
。因此,最后一个附加值将位于数组的开头。

假设作为一个数组(使用您的desire键),您可以使用operator+:

$messages = obj_result + $messages;
从手册中:

while literal keys won't be touched
因此,您只需在元素前面加上前缀,假设
$obj_result
变量的
from
键是一个字符串,所有键都将保持不变,而新元素仍位于数组的开头。

执行类似操作

$array = array("a"=>1,"b"=>2,"d"=>array("e"=>1));
$newArray["c"] = 3;
echo "<pre>";
print_r(array_merge($newArray,$array));
$array=array(“a”=>1,“b”=>2,“d”=>array(“e”=>1));
$newArray[“c”]=3;
回声“;
打印(数组合并($newArray,$array));
在array_merge中,第一个参数将是要在开头添加的键值对