Let's say I append a value to an array, which has been appended to an array:
$a = array();
$b = array();
$b[] = 42;
$a[] = $b;
echo $a[0][0];
>> 42
The output is (as expected) "42". Now let's say I make a small change in the order of my code. This change should make no difference at all, as arrays should be passed by reference.
$a = array();
$b = array();
$a[] = $b;
$b[] = 42;
echo $a[0][0];
>> Notice: Undefined offset: 0 in /opt/iceberg/web/upload/auth.php on line 111
What gives? While reducing my code down to this simplified example, I couldn't shake the feeling that I had made some mistake that should be obvious. Any help would be appreciated!
Solution
You can force a by reference assignment:
$a = array();
$b = array();
$a[] =& $b;
$b[] = 42;
echo $a[0][0];
>> 42
Addendum: Upon usage in more complicated cases, =&
works in mysterious ways and causes some very strange (to me) behaviour. I think for PHP novices, it's best to stick with the by value assignment.