【问题标题】:PHP multidimensional array to html with recursionPHP多维数组到带有递归的html
【发布时间】:2013-09-06 20:20:05
【问题描述】:

我有一个多维数组和两个用于解析抛出数组的函数。我想接收多级无序列表。我的 html 标签有错误,但我找不到。 数组是:

Array
(
    [2] => Array
        (
            [id] => 2
            [parent_id] => 0
            [name] => task2
            [childs] => Array
                (
                    [1] => Array
                        (
                            [id] => 1
                            [parent_id] => 2
                            [name] => task1
                        )
                )
          )
        [3] => Array
        (
            [id] => 3
            [parent_id] => 0
            [name] => task3
            [childs] => Array
                (
                    [4] => Array
                        (
                            [id] => 4
                            [parent_id] => 3
                            [name] => task4
                        )
                    [5] => Array
                        (
                            [id] => 5
                            [parent_id] => 3
                            [name] => task5
                            [childs] => Array
                                (
                                    [6] => Array
                                        (
                                            [id] => 6
                                            [parent_id] => 5
                                            [name] => task6
                                        )
                                 )
                        )
                )
        )
 )

正确的函数是:

function formatHtmlARC11($array) {

foreach ($array as $k => $v) {

    if (is_array($v['childs']) && !empty($v['childs'])) {
       echo $v['id']; 
       $sub=$this->formatHtmlARC11($v['childs']);
    } else {
       echo  $v['id'];
    }
  }
  return $var;
}

我的formatHtml函数有问题的是:

function formatHtmlARC($array,$bul) {
     $htmlcode .='<ul>';
    if($bul==true){
        $htmlcode .='</ul>';  
        $bul=false;
    }

    foreach ($array as $k => $v) {
        if (is_array($v['childs']) && !empty($v['childs'])) {
            $htmlcode .='<li>';  
            $htmlcode .= $v['id']; 
            $htmlcode .='</li>';
            $bul=true;
            $sub=$this->formatHtmlARC($v['childs'], $bul);
        } else {
            $htmlcode .='<li>';  
            $htmlcode .= $v['id']; 
             $htmlcode .='</li>';
         }
       $htmlcode .='</ul>';
    }
    return $htmlcode;
   }  

【问题讨论】:

  • 可以添加输出的 HTML 吗?
  • 我没有问题函数的结果。从正确的函数结果是:213456.

标签: php html multidimensional-array


【解决方案1】:

您的代码似乎有很多问题,很难理解。

我会用更简单的方式来解决这个问题:

$a = [
    ['id' => 1, 'childs' => [
        ['id' => 11],
        ['id' => 12]
    ]],
    ['id' => 2, 'childs' => [
        ['id' => 21],
        ['id' => 22, 'childs' => [
            ['id' => 221],
            ['id' => 222]
        ]]
    ]]
];

function makeListItems($a) {
    $out = '';
    foreach($a as $item) {
        $out .= '<li>';
        $out .= $item['id'];
        if(array_key_exists('childs', $item)) {
            $out .= makeList($item['childs']);
        }
        $out .= '</li>';
    }

    return $out;
}

function makeList($a) {
    $out = '<ul>';
    $out .= makeListItems($a);
    $out .= '</ul>';

    return $out;
}

echo makeList($a);

【讨论】:

  • 谢谢。这种方法是天才和简单的。完美运行。你是大师。
【解决方案2】:

当你向下递归时,你并没有开始一个新的&lt;li&gt;。例如您的代码正在生成

<ul>
   <li>foo</li>
   <li>foo</li>
   <ul>
      etc...
   </ul>
</ul>

这是不正确的。子列表本身也必须包含在&lt;li&gt;

<ul>
   <li>foo</li>
   <li>foo</li>
   <li>
      <ul>
          ...
      </ul>
  </li>
</ul>

所以把你的代码改成

   if (is_array($v['childs']) && !empty($v['childs'])) {
        $htmlcode .='<li>';  
        $htmlcode .= $v['id']; 
        $htmlcode .='</li>';    // remove this line
        $htmlcode .= '<ul>'; // add this line
        $bul=true;
        $sub=$this->formatHtmlARC($v['childs'], $bul);
        $htmlcode .= '</ul></li>';  // add this line

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2012-12-10
    • 2020-03-21
    • 2014-10-31
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多