递归数组列表类别

Recursive array listing catetegory

我有一个多维数组:

Array
(
    [0] => Array
    (
        [category_id] => 20
        [category_name] => Category 1
        [main_category] => 0
        [sub_categories] => Array
            (

                [0] => Array
                    (
                        [category_id] => 25
                        [category_name] => Category 1.3
                        [main_category] => 20
                        [sub_categories] => 
                    )

                [1] => Array
                    (
                        [category_id] => 26
                        [category_name] => Category 1.4
                        [main_category] => 20
                        [sub_categories] => Array
                            (
                                [0] => Array
                                    (
                                        [category_id] => 30
                                        [category_name] => Category 1.4.1
                                        [main_category] => 26
                                        [sub_categories] => 
                                    )

                                [1] => Array
                                    (
                                        [category_id] => 31
                                        [category_name] => Category 1.4.2
                                        [main_category] => 26
                                        [sub_categories] => 
                                    )

                            )

                    )
    [1] => Array
        (
            [category_id] => 19
            [category_name] => Category 2
            [main_category] => 0
            [sub_categories] => 
        )
)

会变成:

Array
(
    [0] => Array
    (
        [category_id] => 20
        [category_name] => Category 1
    )
    [1] => Array
    (
        [category_id] => 25
        [category_name] => Category 1 -> Category 1.3
    )
    [2] => Array
    (
        [category_id] => 26
        [category_name] => Category 1 -> Category 1.4
    )
    [3] => Array
    (
        [category_id] => 30
        [category_name] => Category 1 -> Category 1.4 -> Category 1.4.1
    )
    [4] => Array
    (
        [category_id] => 31
        [category_name] => Category 1 -> Category 1.4 -> Category 1.4.2
    )
    [5] => Array
    (
         [category_id] => 19
         [category_name] => Category 2
    )
)

您可以以递归方式遍历所有数据。在这样做的同时,将所有祖先传递给下一级子级并将它们全部添加到一个新变量中,比如 $result 并在最后打印它们。

片段:

function getChildren($data,$parents,&$result){
    foreach($data as $current_category){
        $temp = [];
        $temp['category_id'] = $current_category['category_id'];
        $new_cat_sequence =  array_merge($parents,[$current_category['category_name']]);
        $temp['category_name'] = implode(" -> ",$new_cat_sequence);
        $result[] = $temp;
        getChildren($current_category['sub_categories'],$new_cat_sequence,$result);
    }
}

演示: https://3v4l.org/QN4NP

你也可以使用这个功能。更容易理解。

function getCats($data, array &$ar, $res = ''){ 
    foreach($data as $arr){ 
        // build a new Categ-s string
        $res_new = ''; 
        $res_new .= $arr['category_name'] && $res ? 
               $res.' -> '.$arr['category_name'] : 
               $arr['category_name']; 

        // pushing data to the resultant array
        $ar[] = ["category_id"=>$arr['category_id'], "category_name"=>$res_new, ];

        // if it has sub_cat-s - use recursion
        if(is_array($arr['sub_categories']) && count($arr['sub_categories'])>0){   
            getCats($arr['sub_categories'], $ar, $res_new);  
        }  
    }
}

$ares = [];
getCats($data,$ares);
print_r($ares);

Demo