gpt4 book ai didi

PHP 深度扩展数组

转载 作者:IT王子 更新时间:2023-10-28 23:57:54 25 4
gpt4 key购买 nike

如何对多维关联数组进行深度扩展(用于解码的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com