2011-10-18 47 views
0

我有一个数据结构到我的数组$类如何在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); 
      } 
     } 
    } 
} 

我知道什么是错的,但我无法猜测。有人可以帮助我多维数组迭代?

+0

注意。在上面的例子中,我有一个两维数组,但可以像用户一样有很多维数。这两个尺寸不是标准。 –

+1

为什么你知道它有什么问题? – str

+1

请将您的代码*在这里*。 – deceze

回答

0

你调用函数与两个参数:

print_category_tree($categ, $forum_id); 

但你的函数只有一个PARAM:

function print_category_tree(&$c) 

,你在功能使用&。您可能不想测试它。

function print_category_tree($c) 
+0

我已经删除了许多部分用于简化查找的正常代码。我只留下了获取问题 –

+0

的RID所需的代码部分,并考虑了新代码 –