phant0m Posted July 2, 2009 Share Posted July 2, 2009 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; } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 2, 2009 Share Posted July 2, 2009 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); ?> Quote Link to comment Share on other sites More sharing options...
phant0m Posted July 2, 2009 Author Share Posted July 2, 2009 hmm yeah, it seems to work anyway, I have solved my problem, accidentlly I referenced a reference and therefore changed some things unintentionally. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.