PHP references when used in recursive arrays
Posted: Thu Apr 21, 2005 9:09 am
hi,
does anyone have a good, sensible and logical explanation for the way php manages references in recursive arrays ?
here is a bit of code adapted from the PHP manual :
here is the output of the code above, what i don't understand is why the contents of $top [ 'A' ] [ 'parent' ] and $top [ 'B' ] [ 'parent' ] are not the same :
thanks for your help,
tepp.
does anyone have a good, sensible and logical explanation for the way php manages references in recursive arrays ?
here is a bit of code adapted from the PHP manual :
Code: Select all
<html><body><pre>
<?php
$top = array (
'A' => array (),
'B' => array ()
);
$top ї 'A' ] ї 'parent' ] = &$top;
$top ї 'B' ] ї 'parent' ] = &$top;
echo "e;\n\n*******A************\n\n"e;;
print_r ( $top ї 'A' ] ї 'parent' ] ); // output: Array ( їA] => Array ( їparent] => Array *RECURSION* ) їB] => Array () )
echo "e;\n\n*******B************\n\n"e;;
print_r ( $top ї 'B' ] ї 'parent' ] ); // output: Array ( їA] => Array ( їparent] => Array ( їA] => Array *RECURSION* їB] => Array () ) ) їB] => Array ( їparent] => Array *RECURSION* ) )
?>
</pre></body></html>Code: Select all
*******A************
Array
(
їA] => Array
(
їparent] => Array
*RECURSION*
)
їB] => Array
(
)
)
*******B************
Array
(
їA] => Array
(
їparent] => Array
(
їA] => Array
*RECURSION*
їB] => Array
(
)
)
)
їB] => Array
(
їparent] => Array
*RECURSION*
)
)tepp.