Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类别的树列表_Php_Wordpress - Fatal编程技术网

Php 类别的树列表

Php 类别的树列表,php,wordpress,Php,Wordpress,我有一个这样的数组 Array ( [0] => stdClass Object ( [cat_id] => 3 [cat_name] => sample 3 [cat_description] => [cat_folder] => sample_3 [cat_path] => sample_3 [cat_parent] => 0 [

我有一个这样的数组

Array
(
[0] => stdClass Object
    (
        [cat_id] => 3
        [cat_name] => sample 3
        [cat_description] => 
        [cat_folder] => sample_3
        [cat_path] => sample_3
        [cat_parent] => 0
        [cat_num_files] => 0
        [cat_num_files_total] => 0
        [cat_user_roles] => 
        [cat_owner] => 1
        [cat_icon] => 
        [cat_exclude_browser] => 0
        [cat_order] => 0
    )

[1] => stdClass Object
    (
        [cat_id] => 2
        [cat_name] => sample 2
        [cat_description] => 
        [cat_folder] => sample_2
        [cat_path] => sample_3/sample_2
        [cat_parent] => 3
        [cat_num_files] => 0
        [cat_num_files_total] => 0
        [cat_user_roles] => 
        [cat_owner] => 1
        [cat_icon] => 
        [cat_exclude_browser] => 0
        [cat_order] => 0
    )

)
我需要像这样列出这些

-sample 3
--sample 2
---sample 4

链接深度没有限制,一个类别可能有5个深度,可能有10个。我曾在
foreach
循环中尝试过这一点,但未能检索到类似于树的列表

我建议使用递归,因为您不知道它的嵌套方式。下面是递归函数的示例

function genCat($parentCat){
    echo $parentCat->name;
    if ($parentCat->haveChildren){
        foreach($parentCat->children as $child){
            genCat($child);
        }
    }
}
这只是一个例子,希望能有所帮助