The reference to complex arrays is not recommended in the PHP Manual. The following is the original article in the manual: it is best to copy complex arrays rather than reference them. The following example will not work as expected. PHPcode & lt ;? Php $ toparray (A & gt; array (), B & gt; array (B _ B & gt; array (),); $ to the PHP Manual, the reference of complex arrays is not recommended.
The original text in the manual is as follows:
It is best to copy complex arrays rather than reference them. The following example will not work as expected.
PHP code
array(), 'B' => array( 'B_b' => array(), ),);$top['A']['parent'] = &$top;$top['B']['parent'] = &$top;$top['B']['B_b']['data'] = 'test';print_r($top['A']['parent']['B']['B_b']); // array()?>
The output result is Array ([data] => test). isn't that expected?
$ Top ['A'] ['parent'] is A reference to the $ top variable ($ top ['A'] ['parent'] = & $ top ;)
Under it ['B'] ['B _ B'],
Isn't it defined by $ top ['B'] ['B _ B'] ['data'] = 'test'; [data] => test?
How can I work as expected? What should I do?
------ Solution --------------------
Let's take a look at the php reference transfer address!
------ Solution --------------------
Output the structure of each step to make it clear:
PHP code
$top = array( 'A' => array(), 'B' => array( 'B_b' => array(), ), ); $top['A']['parent'] = &$top; echo ''; print_r($top['A']['parent']); echo '
'; echo ''; $top['B']['parent'] = &$top; echo '
'; echo ''; print_r($top['B']['parent']); echo '
'; echo ''; $top['B']['B_b']['data'] = 'test'; print_r($top['B']['B_b']); echo '
'; echo ''; print_r($top['A']['parent']['B']['B_b']); // array() echo '
'; echo '';