我有一个数据结构到我的数组$类如何在PHP中多维数组中迭代?
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);
}
}
}
}
我知道什么是错的,但我无法猜测。有人可以帮助我多维数组迭代?
注意。在上面的例子中,我有一个两维数组,但可以像用户一样有很多维数。这两个尺寸不是标准。 –
为什么你知道它有什么问题? – str
请将您的代码*在这里*。 – deceze