appending to nested array
I'm working on the "xml parse to keyed-array" post from yesterday. The problem is much more fundamental that parsing the XML. I simply do not understant how php's "referencing" works.
As an example, I try the following:
// start out with an array containing just "0"
$root = array(0);
// make $root the "parent" for the next array to go inside
$parent =& $root;
// add 5 more arrays, one inside the other
for ($i=1; $i<=5; $i++) {
// an array containing just $i
$newArray = array($i);
// push this onto the end of $parent
$parent[] = $newArray;
// make the new array the new parent
$parent =& $newArray;
// show use what $root looks like now
print_r($root);
echo "<br/>";
}
what I am expecting is a growing nest like this:
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 [1] => Array([0] => 2) ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 [1] => Array([0] => 2 [1] => Array([0] => 3)) ) )
...etc.
BUT, instead I just get this:
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
Array ( [0] => 0 [1] => Array ( [0] => 1 ) )
So, something is wrong with my referencing, right?
Thanks for any help.
Andrew
|