我有一个1到1的线性树,其中语言、=>类型、=>产品、=>等;语言有很多类型和类型,有很多产品等等。
我编写了一个递归函数,以返回如下样式的数组:
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等于那个元素depth,那么我就可以构建数据的面包屑了。
谢谢。
发布于 2013-07-25 14:45:06
这里的关键是创建一个可以递归调用的print函数。我建议你这样做
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);
}
}默认情况下,深度参数为0,但当对$ print_recursive‘父’调用时,我们会将其增加1。这样,每次在数组中越深,它就会增加。
https://stackoverflow.com/questions/17860705
复制相似问题