代码之家  ›  专栏  ›  技术社区  ›  Alex Pliutau

PHP数组递归

  •  3
  • Alex Pliutau  · 技术社区  · 14 年前

    我有一个这样的数组:

    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] => ...
                                  )
                   )
    )
    

    你能帮助我吗?我认为这是一个递归。

    2 回复  |  直到 12 年前
        1
  •  3
  •   Sjoerd    14 年前

    这将执行您想要的操作,除非它不将第一个元素(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 ()
            )
    )
    
        2
  •  3
  •   Wrikken    14 年前
    //$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 (i.e. only 1 reference to each datapoint exists
    var_dump($tree);
    

    别忘了 unset 路径:如果不小心的话,引用确实会给你带来难以跟踪的错误。