Manipulating PHP arrays using references like JS objects -
i manipulating array, shown below, in javascript.
<?php $root = array( 'nodes'=>array( '1'=>array( 'id'=>'1', 'nodes'=>array( '4'=>array( 'id'=>'4', 'nodes'=>array( '5'=>array( 'id'=>'5', 'nodes'=>array() ) ) ) ) ), '2'=>array( 'id'=>'2', 'nodes'=>array() ), '3'=>array( 'id'=>'3', 'nodes'=>array() ) ) ); foreach ($root['nodes'] $_node_id => &$_root_node) { $_put_parent = function (&$_node) use (&$_put_parent) { foreach ($_node['nodes'] $_sub_node_id => &$_sub_node) { $_put_parent($_sub_node); $_sub_node['parent'] = $_node; } }; $_root_node['parent'] = null; $_put_parent($_root_node); } echo '<pre>'; var_dump($root['nodes']['1']['nodes']['4']); var_dump($root['nodes']['1']['nodes']['4']['nodes']['5']['parent']); echo '</pre>'; ?>
output:
array(3) { ["id"]=> string(1) "4" ["nodes"]=> &array(1) { [5]=> array(3) { ["id"]=> string(1) "5" ["nodes"]=> array(0) { } ["parent"]=> array(2) { ["id"]=> string(1) "4" ["nodes"]=> *recursion* } } } ["parent"]=> array(3) { ["id"]=> string(1) "1" ["nodes"]=> &array(1) { [4]=> *recursion* } ["parent"]=> null } } array(2) { ["id"]=> string(1) "4" ["nodes"]=> &array(1) { [5]=> array(3) { ["id"]=> string(1) "5" ["nodes"]=> array(0) { } ["parent"]=> *recursion* } } }
$root['nodes']['1']['nodes']['4']['nodes']['5']['parent']
should point of $root['nodes']['1']['nodes']['4']
$root['nodes']['1']['nodes']['4']['nodes']['5']['parent']
have not got 'parent' reference.
i in javascript not understand problem php.
thank you.
i solved using arrayobjects.
<?php $root = new arrayobject(array( 'nodes'=>new arrayobject(array( '1'=>new arrayobject(array( 'id'=>'1', 'nodes'=>new arrayobject(array( '4'=>new arrayobject(array( 'id'=>'4', 'nodes'=>new arrayobject(array( '5'=>new arrayobject(array( 'id'=>'5', 'nodes'=>new arrayobject(array()) )) )) )) )) )), '2'=>array( 'id'=>'2', 'nodes'=>new arrayobject(array()) ), '3'=>new arrayobject(array( 'id'=>'3', 'nodes'=>new arrayobject(array()) )) )) )); foreach ($root['nodes'] $_node_id => $_root_node) { $_put_parent = function ($_node) use (&$_put_parent) { foreach ($_node['nodes'] $_sub_node_id => $_sub_node) { $_put_parent($_sub_node); $_sub_node['parent'] = $_node; } }; $_root_node['parent'] = null; $_put_parent($_root_node); } echo '<pre>'; var_dump($root['nodes']['1']['nodes']['4']); var_dump($root['nodes']['1']['nodes']['4']['nodes']['5']['parent']); echo '</pre>'; ?>
output now:
object(arrayobject)[5] public 'id' => string '4' (length=1) public 'nodes' => object(arrayobject)[6] public 5 => object(arrayobject)[7] public 'id' => string '5' (length=1) public 'nodes' => object(arrayobject)[8] ... public 'parent' => &object(arrayobject)[5] public 'parent' => object(arrayobject)[3] public 'id' => string '1' (length=1) public 'nodes' => object(arrayobject)[4] public 4 => &object(arrayobject)[5] public 'parent' => null object(arrayobject)[5] public 'id' => string '4' (length=1) public 'nodes' => object(arrayobject)[6] public 5 => object(arrayobject)[7] public 'id' => string '5' (length=1) public 'nodes' => object(arrayobject)[8] ... public 'parent' => &object(arrayobject)[5] public 'parent' => object(arrayobject)[3] public 'id' => string '1' (length=1) public 'nodes' => object(arrayobject)[4] public 4 => &object(arrayobject)[5] public 'parent' => null
Comments
Post a Comment