Multidimensional array to HTML unordered list

后端 未结 1 1044
醉酒成梦
醉酒成梦 2021-01-23 08:07

I have the following array which I want to transform into an unordered list (HTML).

Array
(
    [0] => Array
        (
            [url] => /
            [         


        
相关标签:
1条回答
  • 2021-01-23 09:11

    Try a function like this:

    function ToUl($input){
       echo "<ul>";
    
       $oldvalue = null;
       foreach($input as $value){
         if($oldvalue != null && !is_array($value))
            echo "</li>";
         if(is_array($value)){
            ToUl($value);
         }else
            echo "<li>" + $value;
          $oldvalue = $value;
        }
    
        if($oldvalue != null)
          echo "</li>";
    
       echo "</ul>";
    }
    

    [Edit]

    I'll leave the function which creates a li for every array, which is simpler, in case any reader needs it:

    function ToUl($input){
       echo "<ul>";
    
       foreach($input as $value)
         if(is_array($value)){
            echo "<li>";
            ToUl($value);
            echo "</li>";
         }else
            echo "<li>" + $value + "</li>";
    
       echo "</ul>";
    }
    
    0 讨论(0)
提交回复
热议问题