Category hierarchy from array(cat id => parent id)

后端 未结 6 1949
你的背包
你的背包 2021-01-24 23:26

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 subcategor

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 23:37

    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 '
    '.
        print_r($parent_ids, true).
        print_r($end_array,true).
        '
    ' ;

    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
            )
    
    )
    

提交回复
热议问题