3v4l.org

run code in 500+ PHP versions simultaneously
<?php $a = ['p' => ['id' => TRUE]]; $b = ['p' => FALSE]; print "Behaves as expected.\n"; var_dump(array_merge_recursive($a, $b)); print "\n\nOMG internal array pointer shenanigans can affect the results of array_merge_recursive()!\n"; $a['p'][0] = 'SOMETHING, anything!'; unset($a['p'][0]); var_dump(array_merge_recursive($a, $b)); print "\n\nThis keeps happening…\n"; var_dump(array_merge_recursive($a, $b)); print "\n\n… not even resetting the internal array pointer helps …\n"; reset($a['p']); var_dump(array_merge_recursive($a, $b)); print "\n\n… nor casting to object, cloning, then back to array …\n"; $a['p'] = (array) clone (object) $a['p']; var_dump(array_merge_recursive($a, $b)); print "\n\n… only forcefully recreating a copy-by-value of the array by using array_slice().\n"; $a['p'] = array_slice($a['p'], 0); var_dump(array_merge_recursive($a, $b));
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Behaves as expected. array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [0]=> bool(false) } } OMG internal array pointer shenanigans can affect the results of array_merge_recursive()! array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [1]=> bool(false) } } This keeps happening… array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [1]=> bool(false) } } … not even resetting the internal array pointer helps … array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [1]=> bool(false) } } … nor casting to object, cloning, then back to array … array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [1]=> bool(false) } } … only forcefully recreating a copy-by-value of the array by using array_slice(). array(1) { ["p"]=> array(2) { ["id"]=> bool(true) [0]=> bool(false) } }

preferences:
34.12 ms | 1140 KiB | 8 Q