0

I have an multidimensional array and two functions for parsing throw the array. I want to receive multilevel unordered list. I have a mistake with html tags, but I can't find. Array is:

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
                                        )
                                 )
                        )
                )
        )
 )

The correct function is:

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;
}

My formatHtml function with a problem is:

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

Your code seems to have a lot of issues and is very hard to follow.

I'd go about it in a simpler manner:

$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 回答
1

You're not starting a new <li> when you go recurse down. e.g. your code is generating

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

This is incorrect. the child lists must also themselves be contained in a <li>:

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

So change your code to

   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
于 2013-09-06T20:53:47.470 回答