Why this code not works as I expected? Inside the Test(&$array) function I would set the ref parameter to the global $array1 but this didn't works.
$array1 = array();
$array2 = array();
function Test(&$array)
{
global $array1;
$array = &$array1;
$array['inside'] = 'inside';
}
//SET BY THE FUNCTION:
Test($array2);
$array2['test1'] = 'test1';
var_dump($array1); //array('inside' => 'inside') ** WHERE IS THE 'test1' key? **
var_dump($array2); //array('test1' => 'test1') ** WHERE IS THE 'inside' key? **
//SET WITHOUT THE FUNCTION:
$array2 = &$array1;
$array2['test2'] = 'test2';
var_dump($array1); //array('inside' => 'inside', 'test2' => 'test2') ** FINE **
var_dump($array2); //array('inside' => 'inside', 'test2' => 'test2') ** FINE **
EDIT:
It's quite clear that if I changed $array to point to $array1 then $array1 will have the 'inside' => 'inside' value outside the function. What not clear that if I set $array2['test1'] = 'test1' why not change this $array1 also? Its 'linked' before inside the function!