Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
_php-array-merge-ref-preservation - Shows that references are presereved when merging arrays with +=
<?php
$foo = 12;
$a1 = ['foo' => &$foo];
$showa1 = function() use(&$a1) {
print_r($a1);
};
$bar = 100;
$a2 = ['bar' => &$bar];
// $merged = array_merge($a1, $a2);
$a1 += $a2;
// $a1['foo'] = 24;
// $a1['bar'] = 9;
// print assert($foo === 24); // works just fine
// print assert($bar === 9); // works just fine
$a1['foo'] = 24;
$bar = 9;
print assert($foo === 24); // works just fine
print assert($a1['bar'] === 9); // works just fine
$showa1();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment