HosipLan

Untitled

Jan 3rd, 2012
157
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. foreach ($list as $leaf) {
  29.     if ($leaf->parent !== 0) {
  30.         $list[$leaf->parent]->children[] = $leaf;
  31.         unset($list[$leaf->id]);
  32.     }
  33. }
  34.  
  35. echo "<pre>"; print_r($list); echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment