рекурсия массива php

У меня такой массив:

Array ( [0] => Array ( [id] => 1000 [enroller_id] => 1005) [1] => Array ( [id] => 1005 [enroller_id] =>) [2] => Array ( [id] => 1101 [enroller_id] => 1000 ) [3] => Array ( [id] => 1111 [enroller_id] => 1000 ) ) 

Я хочу создать массив иерархии следующим образом:

 Array( [1005] => Array( [1000] => Array( [1101] => ... [1111] => ... ) ) ) 

Вы можете мне помочь? Я думаю, что это рекурсия.

Это сделает то, что вы хотите, за исключением того, что он не помещает первый элемент (1005) в массив:

 function create_array($number, $data) { $result = array(); foreach ($data as $row) { if ($row['enroller_id'] == $number) { $result[$row['id']] = create_array($row['id'], $data); } } return $result; } print_r(create_array(1005, $data)); 

Вывод:

 Array ( [1000] => Array ( [1101] => Array () [1111] => Array () ) ) 
 //$paths is an array of references, in which _every_ item will sit at 'root' //level, but also as a reference as a child to it's parent. //initialize location of parentless / root items: $paths = array('N'=>array()); foreach($items as $item){ //$target is the parent-id, or 'N' if we are a root node $target = isset($item['enroller_id']) && !empty($item['enroller_id']) ? $item['enroller_id'] :'N'; //if the parent is not yet in the paths array, make an entry for it if(!isset($paths[$target])) $paths[$target] = array(); //if this item is not yet in the array (the previous statement could //already have inserted it, make an array( if(!isset($paths[$item['id']])) $paths[$item['id']] = array(); //add the current item as a reference to it's parent $paths[$target][$item['id']] = &$paths[$item['id']]; //Setting it as a reference has this consequence: // when adding an item to the $paths[$id] array, it will // automatically be added to $paths[$parent][$id], as // both $paths[$id] & $paths[$parent][$id] point to the same // location in memory. // This goes to infinite depth: if $foo is a child of $id, and you // add a node to it, it will be in // $paths[$foo] = array($child); // $paths[$id][[$foo] = array($child); // $paths[$parent][$id][$foo] = array($child); // // Altering an item at any location in paths / the tree will alter it anywhere // in the paths / tree, unsetting it anywhere only unset the data at that location, // other locations will still have the same data (and the data will keep // existing until the last reference is unset()) } //we are only interested in the 'root' nodes (all other nodes should be subnodes //in this tree $tree = $paths['N']; //remove all unused references in the $paths array //do remember to do this: cleaning up references is important unset($paths); //tree is now an array of 'normal' values (ie only 1 reference to each datapoint exists var_dump($tree); этот //$paths is an array of references, in which _every_ item will sit at 'root' //level, but also as a reference as a child to it's parent. //initialize location of parentless / root items: $paths = array('N'=>array()); foreach($items as $item){ //$target is the parent-id, or 'N' if we are a root node $target = isset($item['enroller_id']) && !empty($item['enroller_id']) ? $item['enroller_id'] :'N'; //if the parent is not yet in the paths array, make an entry for it if(!isset($paths[$target])) $paths[$target] = array(); //if this item is not yet in the array (the previous statement could //already have inserted it, make an array( if(!isset($paths[$item['id']])) $paths[$item['id']] = array(); //add the current item as a reference to it's parent $paths[$target][$item['id']] = &$paths[$item['id']]; //Setting it as a reference has this consequence: // when adding an item to the $paths[$id] array, it will // automatically be added to $paths[$parent][$id], as // both $paths[$id] & $paths[$parent][$id] point to the same // location in memory. // This goes to infinite depth: if $foo is a child of $id, and you // add a node to it, it will be in // $paths[$foo] = array($child); // $paths[$id][[$foo] = array($child); // $paths[$parent][$id][$foo] = array($child); // // Altering an item at any location in paths / the tree will alter it anywhere // in the paths / tree, unsetting it anywhere only unset the data at that location, // other locations will still have the same data (and the data will keep // existing until the last reference is unset()) } //we are only interested in the 'root' nodes (all other nodes should be subnodes //in this tree $tree = $paths['N']; //remove all unused references in the $paths array //do remember to do this: cleaning up references is important unset($paths); //tree is now an array of 'normal' values (ie only 1 reference to each datapoint exists var_dump($tree); не //$paths is an array of references, in which _every_ item will sit at 'root' //level, but also as a reference as a child to it's parent. //initialize location of parentless / root items: $paths = array('N'=>array()); foreach($items as $item){ //$target is the parent-id, or 'N' if we are a root node $target = isset($item['enroller_id']) && !empty($item['enroller_id']) ? $item['enroller_id'] :'N'; //if the parent is not yet in the paths array, make an entry for it if(!isset($paths[$target])) $paths[$target] = array(); //if this item is not yet in the array (the previous statement could //already have inserted it, make an array( if(!isset($paths[$item['id']])) $paths[$item['id']] = array(); //add the current item as a reference to it's parent $paths[$target][$item['id']] = &$paths[$item['id']]; //Setting it as a reference has this consequence: // when adding an item to the $paths[$id] array, it will // automatically be added to $paths[$parent][$id], as // both $paths[$id] & $paths[$parent][$id] point to the same // location in memory. // This goes to infinite depth: if $foo is a child of $id, and you // add a node to it, it will be in // $paths[$foo] = array($child); // $paths[$id][[$foo] = array($child); // $paths[$parent][$id][$foo] = array($child); // // Altering an item at any location in paths / the tree will alter it anywhere // in the paths / tree, unsetting it anywhere only unset the data at that location, // other locations will still have the same data (and the data will keep // existing until the last reference is unset()) } //we are only interested in the 'root' nodes (all other nodes should be subnodes //in this tree $tree = $paths['N']; //remove all unused references in the $paths array //do remember to do this: cleaning up references is important unset($paths); //tree is now an array of 'normal' values (ie only 1 reference to each datapoint exists var_dump($tree); 

Не забывайте unset пути: ссылки могут действительно укусить вас с трудностью отслеживания ошибок, если вы не будете должным образом заботиться.