Multidimensional array to HTML unordered list

后端 未结 1 1045
醉酒成梦
醉酒成梦 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 "
      "; $oldvalue = null; foreach($input as $value){ if($oldvalue != null && !is_array($value)) echo ""; if(is_array($value)){ ToUl($value); }else echo "
    • " + $value; $oldvalue = $value; } if($oldvalue != null) echo "
    • "; echo "
    "; }

    [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 "
      "; foreach($input as $value) if(is_array($value)){ echo "
    • "; ToUl($value); echo "
    • "; }else echo "
    • " + $value + "
    • "; echo "
    "; }

    0 讨论(0)
提交回复
热议问题