Rumah > pembangunan bahagian belakang > tutorial php > 怀疑:php array_merge_recursive的一个bug

怀疑:php array_merge_recursive的一个bug

WBOY
Lepaskan: 2016-06-06 20:40:19
asal
1344 orang telah melayarinya

<code>$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);
$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);
$adiff = array_merge_recursive($diff,$adiff);
var_dump($adiff);
</code>
Salin selepas log masuk
Salin selepas log masuk

理论上应该返回:

<code>array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }
</code>
Salin selepas log masuk
Salin selepas log masuk

实际返回:

<code>array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }
</code>
Salin selepas log masuk
Salin selepas log masuk

这算不算一个bug?

更新:
这个不算bug,只是手册里面没有对数字键进行merge时会重置说明清楚。

回复内容:

<code>$adiff = json_decode('{"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0}',true);
$diff = json_decode('{"28":1,"29":1,"30":1,"31":1,"32":1,"33":1,"34":1,"35":1}',true);
$adiff = array_merge_recursive($diff,$adiff);
var_dump($adiff);
</code>
Salin selepas log masuk
Salin selepas log masuk

理论上应该返回:

<code>array(16) { [28]=> int(1) [29]=> int(1) [20]=> int(1) [31]=> int(1) [32]=> int(1) [33]=> int(1) [34]=> int(1) [35]=> int(1) [36]=> int(0) [37]=> int(0) [38]=> int(0) [39]=> int(0) [40]=> int(0) [41]=> int(0) [42]=> int(0) [43]=> int(0) }
</code>
Salin selepas log masuk
Salin selepas log masuk

实际返回:

<code>array(16) { [0]=> int(1) [1]=> int(1) [2]=> int(1) [3]=> int(1) [4]=> int(1) [5]=> int(1) [6]=> int(1) [7]=> int(1) [8]=> int(0) [9]=> int(0) [10]=> int(0) [11]=> int(0) [12]=> int(0) [13]=> int(0) [14]=> int(0) [15]=> int(0) }
</code>
Salin selepas log masuk
Salin selepas log masuk

这算不算一个bug?

更新:
这个不算bug,只是手册里面没有对数字键进行merge时会重置说明清楚。

手册中提到了:

If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

如果两个数组存在相同的数字键名,则会在数组最后新建一个索引(虽然手册中没提到索引会重置)。

这种情况下推荐使用array_replace_recursive(PHP≥5.3)。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan