Jump to content

[SOLVED] "Pointers?" - reference in array to itself


phant0m

Recommended Posts

Hello

 

I've got an array which basically represents a hierarchy of nodes. Each node can have children of its own.

Now, for easier access, I wanted to reference the parent node inside the child node, unfortunately it doesn't seem to work.

 

Simplified code:

<?php
foreach($parent['children'] as &$child){
  $child['parent'] = &$parent;
}

?>

works for me:

 

<pre>
<?php
$parent = array(
  'name' => 'I am the parent',
  'children' => array(
    array(
      'name' => 'I am the first child',
    ),
    array(
      'name' => 'I am the second child',
    ),
    array(
      'name' => 'I am the third child',
    ),
  ),
);

foreach($parent['children'] as &$child){
  $child['parent'] = &$parent;
}

print $parent['children'][1]['parent']['name'];
print_r($parent);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.