0

多次元配列と、配列をスローする解析用の 2 つの関数があります。マルチレベルの順序なしリストを受け取りたいです。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;
   }  
4

2 に答える 2

3

あなたのコードには多くの問題があるようで、従うのは非常に困難です。

私はもっ​​と簡単な方法でそれについて行きます:

$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);
于 2013-09-06T20:51:51.137 に答える