如何在PHP中迭代多维数组?
我将该数据结构放入数组$categories中如何在PHP中迭代多维数组?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我将该数据结构放入数组$categories中 stdClass Object ( [37] => Array ( [term_id] => 37 [name] => Files [parent] => 0 [38] => Array ( [term_id] => 38
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);
}
}
}
}
$categories=get_array_content();//返回上述数据
打印目录树($categories);
函数打印目录树(&$c)
{
foreach($c作为$cat)
{
?>
使用使用两个参数调用函数:
print_category_tree($categ, $forum_id);
但您的函数只接受一个参数:
function print_category_tree(&$c)
并且您正在函数中使用-。您可能希望在不使用的情况下测试它
function print_category_tree($c)
注意。在上面的示例中,我有一个二维数组,但它可以是用户喜欢的任意多个维度。二维不是标准的。为什么你知道它有问题?请在这里发布你的代码。因为不起作用。我尝试了几个不同的引用,但什么都没有。我还注意到脚本的速度很快循环速度非常慢。我的意思是大约需要30到40秒才能完成可能的重复读取。为了简化外观,我删除了许多普通代码的部分。我只保留了解决问题所需的部分代码