Php 数组树的线性表示

Php 数组树的线性表示,php,multidimensional-array,recursive-datastructures,Php,Multidimensional Array,Recursive Datastructures,我有一个1对1的线性树,其中语言=>types=>products=>etc;语言有多种类型,类型有多种产品等等 我编写了一个递归函数,以返回以下样式的数组: Array ( [0] => Array ( [id] => 166 [name] => product1 [type] => product [depth] => 2 [parent] => Array

我有一个1对1的线性树,其中语言=>types=>products=>etc;语言有多种类型,类型有多种产品等等

我编写了一个递归函数,以返回以下样式的数组:

Array
(
    [0] => Array
    (
        [id] => 166
        [name] => product1
        [type] => product
        [depth] => 2
        [parent] => Array
            (
                [0] => Array
                    (
                        [id] => 165
                        [name] => default
                        [type] => type
                        [depth] => 1
                        [parent] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 1
                                        [name] => en
                                        [type] => language
                                        [depth] => 0
                                        [parent] => false

                                    )

                            )

                    )

            )

    )

)
我想要的是一个递归方法,它将遍历该树并提供一个数组,如

[0] => array( 'id' => 1, 'name' => 'en'),
[1] => array( 'id' => 165, 'name' => 'default'),
[2] => array( 'id' => 166, 'name' => 'product1')
由于0,1,2等于元素
深度
,所以我可以构建数据的面包屑


谢谢。

这里的关键是制作一个可以递归调用的打印函数。我建议这样做

function print_recursive($array, $depth = 0) {
    //Code to print your stuff

    //Calls the print function on the parent if it's an array
    if(is_array($array['parent'])) {
        print_recursive($array['parent'], $depth+1);
    }
}

默认情况下,depth参数为0,但在$array['parent']上调用print_recursive时,我们将其递增1。这样,每次深入数组时,它都会增加。

这里的关键是制作一个可以递归调用的打印函数。我建议这样做

function print_recursive($array, $depth = 0) {
    //Code to print your stuff

    //Calls the print function on the parent if it's an array
    if(is_array($array['parent'])) {
        print_recursive($array['parent'], $depth+1);
    }
}

默认情况下,depth参数为0,但在$array['parent']上调用print_recursive时,我们将其递增1。这样,每次深入数组时,它都会递增。

谢谢,我今天过了很长时间,递归函数突然变得难以想象。在你的帮助下,我完成了这项工作,并将其纳入了我项目的其他部分。谢谢你,我度过了漫长的一天,递归函数突然变得难以想象。在你的帮助下,我完成了这项工作,并将其重新纳入了我项目的其他部分。