fork download
  1. <?php
  2.  
  3. $root = new ArrayObject(array(
  4. 'nodes'=>new ArrayObject(array(
  5. '1'=>new ArrayObject(array(
  6. 'id'=>'1',
  7. 'nodes'=>new ArrayObject(array(
  8. '4'=>new ArrayObject(array(
  9. 'id'=>'4',
  10. 'nodes'=>new ArrayObject(array(
  11. '5'=>new ArrayObject(array(
  12. 'id'=>'5',
  13. 'nodes'=>new ArrayObject(array())
  14. ))
  15. ))
  16. ))
  17. ))
  18. )),
  19. '2'=>array(
  20. 'id'=>'2',
  21. 'nodes'=>new ArrayObject(array())
  22. ),
  23. '3'=>new ArrayObject(array(
  24. 'id'=>'3',
  25. 'nodes'=>new ArrayObject(array())
  26. ))
  27. ))
  28. ));
  29.  
  30. foreach ($root['nodes'] as $_node_id => $_root_node) {
  31. $_put_parent = function ($_node) use (&$_put_parent) {
  32. foreach ($_node['nodes'] as $_sub_node_id => $_sub_node) {
  33. $_put_parent($_sub_node);
  34. $_sub_node['parent'] = $_node;
  35. }
  36. };
  37.  
  38. $_root_node['parent'] = null;
  39. $_put_parent($_root_node);
  40. }
  41.  
  42. echo '<pre>';
  43. var_dump($root['nodes']['1']['nodes']['4']);
  44. var_dump($root['nodes']['1']['nodes']['4']['nodes']['5']['parent']);
  45. echo '</pre>';
  46.  
  47. ?>
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
<pre>object(ArrayObject)#5 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    ["id"]=>
    string(1) "4"
    ["nodes"]=>
    object(ArrayObject)#6 (1) {
      ["storage":"ArrayObject":private]=>
      array(1) {
        [5]=>
        object(ArrayObject)#7 (1) {
          ["storage":"ArrayObject":private]=>
          array(3) {
            ["id"]=>
            string(1) "5"
            ["nodes"]=>
            object(ArrayObject)#8 (1) {
              ["storage":"ArrayObject":private]=>
              array(0) {
              }
            }
            ["parent"]=>
            *RECURSION*
          }
        }
      }
    }
    ["parent"]=>
    object(ArrayObject)#3 (1) {
      ["storage":"ArrayObject":private]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["nodes"]=>
        object(ArrayObject)#4 (1) {
          ["storage":"ArrayObject":private]=>
          array(1) {
            [4]=>
            *RECURSION*
          }
        }
        ["parent"]=>
        NULL
      }
    }
  }
}
object(ArrayObject)#5 (1) {
  ["storage":"ArrayObject":private]=>
  array(3) {
    ["id"]=>
    string(1) "4"
    ["nodes"]=>
    object(ArrayObject)#6 (1) {
      ["storage":"ArrayObject":private]=>
      array(1) {
        [5]=>
        object(ArrayObject)#7 (1) {
          ["storage":"ArrayObject":private]=>
          array(3) {
            ["id"]=>
            string(1) "5"
            ["nodes"]=>
            object(ArrayObject)#8 (1) {
              ["storage":"ArrayObject":private]=>
              array(0) {
              }
            }
            ["parent"]=>
            *RECURSION*
          }
        }
      }
    }
    ["parent"]=>
    object(ArrayObject)#3 (1) {
      ["storage":"ArrayObject":private]=>
      array(3) {
        ["id"]=>
        string(1) "1"
        ["nodes"]=>
        object(ArrayObject)#4 (1) {
          ["storage":"ArrayObject":private]=>
          array(1) {
            [4]=>
            *RECURSION*
          }
        }
        ["parent"]=>
        NULL
      }
    }
  }
}
</pre>