dongyan5141 2014-11-27 04:03
浏览 84
已采纳

数组中的类别层次结构(cat id => parent id)

i am trying to create a multidimensional array hierarchy from a simple array which contains pairs of category ids and parent ids. The categories can be a parent and a subcategory at the same time. The base categories have a parent of 0 (=no parent). For example:

# cat_id => parent_id
$initialArray = array(
  1 => 0,
  2 => 1,
  3 => 2,

  4 => 0,
  5 => 4,

  6 => 0
);

From this, i'd like to get an array that represents a structure like this:

  • 1
    • 2
      • 3
  • 4
    • 5
  • 6

I will not know the contents of $initialArray beforehand.

I tried to look at other similar questions but i couldn't find an answer. Please help!

  • 写回答

6条回答 默认 最新

  • dougan6982 2014-11-27 05:16
    关注

    Well it appears to me you need a recursive function. Assuming everything has a parent or value beginning at the base level of 0, I resituated the array to have all parent ids listing their children rather than the other way around above. After that, I created a recursive function.

    $initialArray = array(
        1 => 0,
        2 => 1,
        3 => 2,
    
        4 => 0,
        5 => 4,
    
        6 => 0
    );
    
    // resituate the array
    $parent_ids = array();
    foreach ($initialArray as $category_id => $parent_id) {
        if (!isSet($parent_ids[$parent_id])) {
            $parent_ids[$parent_id] = array();
        }
        $parent_ids[$parent_id][] = $category_id;
    }
    
    // end_array is the result
    $end_array = array();
    
    /**
     * Takes the key of the parent, the current set that it's working off of, the list of parent ids for reference
     * and the current place in the end result array, acting recursively
     */
    function recursive($parent_key, $current_set, $parent_ids, $end_array) {
        foreach ($current_set as $parent_value) {
            if (!isSet($parent_ids[$parent_value])) {
                $end_array[$parent_key][] = $parent_value;
            } else {
                // if the parent_value is found in parent_ids, pass those values to the same function and the current end_array position
                $end_array[$parent_key] = recursive($parent_value, $parent_ids[$parent_value], $parent_ids, $end_array[$parent_key]);
            }
        }
        return $end_array;
    }
    // start with the top most element
    $end_array = recursive(key($parent_ids), current($parent_ids), $parent_ids, $end_array);
    
    print '<pre>'.
        print_r($parent_ids, true).
        print_r($end_array,true).
        '</pre>'
    ;
    

    Outputs:

    // resituated array
    Array
    (
        [0] => Array
            (
                [0] => 1
                [1] => 4
                [2] => 6
            )
    
        [1] => Array
            (
                [0] => 2
            )
    
        [2] => Array
            (
                [0] => 3
            )
    
        [4] => Array
            (
                [0] => 5
            )
    
    )
    
    // the end result
    Array
    (
        [0] => Array
            (
                [1] => Array
                    (
                        [2] => Array
                            (
                                [0] => 3
                            )
    
                    )
    
                [4] => Array
                    (
                        [0] => 5
                    )
    
                [5] => 6
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效