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);
}
}
}
这只是一个例子,希望能有所帮助