Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $list = array(
- 1 => (object)array(
- 'id' => 1,
- 'parent' => 0
- ),
- 2 => (object)array(
- 'id' => 2,
- 'parent' => 0
- ),
- 3 => (object)array(
- 'id' => 3,
- 'parent' => 2
- ),
- 4 => (object)array(
- 'id' => 4,
- 'parent' => 2
- ),
- 5 => (object)array(
- 'id' => 5,
- 'parent' => 2
- ),
- 6 => (object)array(
- 'id' => 6,
- 'parent' => 5
- ),
- );
- foreach ($list as $leaf) {
- if ($leaf->parent !== 0) {
- $list[$leaf->parent]->children[] = $leaf;
- unset($list[$leaf->id]);
- }
- }
- echo "<pre>"; print_r($list); echo "</pre>";
Advertisement
Add Comment
Please, Sign In to add comment