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;
   }  
2
  • Could you add the outputed HTML? Commented Sep 6, 2013 at 20:41
  • I haven't a result from a problem function. From the correct function the result is: 213456. Commented Sep 6, 2013 at 20:43

2 Answers 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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank's. This methods is genios and simple. Works perfect. You are a guru.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.