我把这个数据结构放入数组$categories
stdClass Object
(
[37] => Array
(
[term_id] => 37
[name] => Files
[parent] => 0
[38] => Array
(
[term_id] => 38
[name] => Downloads
[parent] => 37
)
)
[29] => Array
(
[term_id] => 29
[name] => Articles
[parent] => 0
[36] => Array
(
[term_id] => 36
[name] => General
[parent] => 29
)
[35] => Array
(
[term_id] => 35
[name] => WordPress
[parent] => 29
)
[34] => Array
(
[term_id] => 34
[name] => Php, Sql
[parent] => 29
)
)
[1] => Array
(
[term_id] => 1
[name] => Uncategorized
[parent] => 0
)
)
现在我要做的是将这些数据以下面的格式打印出来,并带缩进。
Files
Downloads
Articles
General
WordPress
Php, Sql
Uncategorized
为了达到上面的结果,我使用了代码
$categories = get_array_content(); // Return the above data
print_category_tree($categories);
function print_category_tree(&$c)
{
foreach($c as $cat)
{
?>
<label>
<input type="checkbox" value="<?php echo $cat['term_id']; ?>" /> <?php echo $cat['name']; ?>
</label>
<br />
<?php
foreach($cat as $categ)
{
if(is_array($categ))
{
print_category_tree($categ);
}
}
}
}
我知道那有问题,但我猜不出是什么。谁能帮我一下多维数组迭代?
使用http://www.php.net/manual/en/class.recursivearrayiterator.php
你用两个参数来调用这个函数:
print_category_tree($categ, $forum_id);
但是函数只有一个参数:
function print_category_tree(&$c)
你正在使用&在函数中。您可能需要测试它,而不需要。
function print_category_tree($c)