HosipLan

Untitled

Jan 3rd, 2012
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $list = array(
  2.     1 => (object)array(
  3.         'id' => 1,
  4.         'parent' => 0
  5.     ),
  6.     2 => (object)array(
  7.         'id' => 2,
  8.         'parent' => 0
  9.     ),
  10.     3 => (object)array(
  11.         'id' => 3,
  12.         'parent' => 2
  13.     ),
  14.     4 => (object)array(
  15.         'id' => 4,
  16.         'parent' => 2
  17.     ),
  18.     5 => (object)array(
  19.         'id' => 5,
  20.         'parent' => 2
  21.     ),
  22.     6 => (object)array(
  23.         'id' => 6,
  24.         'parent' => 5
  25.     ),
  26. );
  27.  
  28. $arr = array();
  29. foreach ($list as $leaf) {
  30.     if ($leaf->parent !== 0) {
  31.         $list[$leaf->parent]->children[] = $leaf;
  32.  
  33.     } else {
  34.         $arr[] = $leaf;
  35.     }
  36. }
  37.  
  38. echo "<pre>"; print_r($arr); echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment