PHP Deep Extend Array

后端 未结 7 1685
遇见更好的自我
遇见更好的自我 2020-12-31 01:53

How can I do a deep extension of a multi dimensional associative array (for use with decoded JSON objects). I need the php equivalent of jQuery\'s $.extend(true, array1, arr

7条回答
  •  执笔经年
    2020-12-31 02:01

    If you have PHP 5.3.0+, you can use array_replace_recursive which does exactly what you need:

    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.

提交回复
热议问题