1

I have this array:

Array ( 
    [0] => 17-GUIDO HUMBERTO -3 
    [1] => 
    Array ( 
        [0] => 2-José-3 
          ) 
    [2] => 
    Array ( 
        [0] => 
            Array ( 
                [0] => 18-juan andres-3 
                  ) 
          ) 
) 

I'm trying to put that array in a list with ul li like this:

  • 17-GUIDO HUMBERTO -3
    • 2-José-3
      • 18-juan andres-3

I dont know how to do it, I do not know if the array is correct, please i need help. Thanks!

ccKep
  • 5,786
  • 19
  • 31
Jesús Cova
  • 339
  • 3
  • 18
  • Possible duplicate of [PHP foreach with Nested Array?](http://stackoverflow.com/questions/3684463/php-foreach-with-nested-array) – Eoghan Jan 31 '17 at 02:41

1 Answers1

0

You need a thing called 'recursion', when you have a function that calls itself, e.g.:

<?php
$arr = array(
    '17-GUIDO HUMBERTO -3 ', 
    array( 
        '2-José-3'
    ),
    array(
        array(
            '18-juan andres-3'
        )
    )
);


function listArr($arr) {
    $html = '<ul>';
    foreach ($arr as $item) {
        if (is_array($item)) {
            $html .= listArr($item); // <<< here is the recursion
        } else {
            $html .= '<li>' . $item . '</li>';
        }
    }

    $html .= '</ul>';
    return $html;
}

echo(listArr($arr));