PHP 深度扩展数组

标签 php jquery json array-merge

如何对多维关联数组进行深度扩展(用于解码的 JSON 对象)。 我需要 jQuery's $.extend(true, array1, array2) 的 php 等价物使用数组而不是 JSON 和 PHP。

这是我需要的一个示例(array_merge_recursive 似乎没有做同样的事情)

$array1 = ('1'=> ('a'=>'array1a', 'b'=>'array1b'));
$array2 = ('1'=> ('a'=>'array2a', 'c'=>'array2b'));

$array3 = array_extend($array1, $array2);

//$array3 = ('1'=> ('a'=>'array2a', 'b'=>'array1b', 'c'=>'array2b'))

如果 array2 具有相同的值,请注意它是如何覆盖 array1 的(例如类扩展的工作原理)

最佳答案

如果你有 PHP 5.3.0+,你可以使用 array_replace_recursive这正是您需要的:

array_replace_recursive() replaces the values of array1 with the same values from all the following arrays. If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not the first, it will be created in the first array. If a key only exists in the first array, it will be left as is. If several arrays are passed for replacement, they will be processed in order, the later array overwriting the previous values.

关于PHP 深度扩展数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12725113/

相关文章:

php - 同时使用 PDO 准备好的语句和 filter_var?

javascript - 当我运行 npm start 时,我在 ubuntu 16.04 中收到 npm 错误

php - 插入数组值取决于带条件的数据表(嵌套数组)

jquery - 移动浏览器上的粘性 header

java - 将数据写入json文件

PHP - 加载图像(图像位于服务器上)并作为 BLOB 插入数据库

javascript - 如何使用 parsley.js 提交表单?

php - 有没有更好的(轻量级)解决方案,可以在没有 jquery ui 的情况下使用 php 和 codeigniter 自动完成?

android - 如何请求 URL 中有空格的 JSON?

json - Cosmos/Document Db 客户端忽略枚举的 JsonConverter