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; } ?> Link to comment https://forums.phpfreaks.com/topic/164518-solved-pointers-reference-in-array-to-itself/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/164518-solved-pointers-reference-in-array-to-itself/#findComment-867759 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. Link to comment https://forums.phpfreaks.com/topic/164518-solved-pointers-reference-in-array-to-itself/#findComment-867780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.