递归ulli到PHP多维数组

递归ulli到PHP多维数组,php,tree,html-lists,Php,Tree,Html Lists,我正在尝试转换一个HTML块,它基本上是以下形式的(每个列表项应该在一行上,因此如果您明白我的意思,那么不应该有任何包含的行): 实现这一点的最简单方法是使用递归函数,如下所示: //output a multi-dimensional array as a nested UL function toUL($array){ //start the UL echo "<ul>\n"; //loop through the array foreach

我正在尝试转换一个HTML块,它基本上是以下形式的(每个列表项应该在一行上,因此如果您明白我的意思,那么不应该有任何包含
  • 的行):


    实现这一点的最简单方法是使用递归函数,如下所示:

    //output a multi-dimensional array as a nested UL
    function toUL($array){
        //start the UL
        echo "<ul>\n";
           //loop through the array
        foreach($array as $key => $member){
            //check for value member
            if(isset($member['value']) ){
                //if value is present, echo it in an li
                echo "<li>{$member['value']}</li>\n";
            }
            else if(is_array($member)){
                //if the member is another array, start a fresh li
                echo "<li>\n";
                //and pass the member back to this function to start a new ul
                toUL($member);
                //then close the li
                echo "</li>\n";
            }
        }
        //finally close the ul
        echo "</ul>\n";
    }
    
    //将多维数组作为嵌套数组输出
    函数toUL($array){
    //启动UL
    回声“
      \n”; //在数组中循环 foreach($key=>$member的数组){ //检查成员的值 如果(isset($member['value'])){ //如果值存在,则在li中回显它 echo“
    • {$member['value']}
    • \n”; } else if(is_数组($member)){ //如果成员是另一个数组,则启动新的li 回声“
    • \n”; //并将成员传递回此函数以启动新的ul 图尔(成员); //然后关上电梯 回声“
    • \n”; } } //最后关闭ul 回声“
    \n”; }
    将数组传递给该函数,使其以所需的方式输出

    希望有帮助

    问候,,
    菲尔,

    如果以后有人遇到这个问题,这就是答案

    function ul_to_array($ul){
            if(is_string($ul)){
                if(!$ul = simplexml_load_string($ul)) {
                    trigger_error("Syntax error in UL/LI structure");
                    return FALSE;
                }
                return ul_to_array($ul);
            } else if(is_object($ul)){
                $output = array();
                foreach($ul->li as $li){
                    $update_with = (isset($li->ul)) ? ul_to_array($li->ul) : (($li->count()) ? $li->children()->asXML() : (string) $li);
                    if(is_string($update_with)){
                        if(trim($update_with) !== "" && $update_with !== null){
                            $output[] = $update_with;
                        }
                    } else {
                            $output[] = $update_with;
                    }
                }
                return $output;
            } else {
                return FALSE;
            }
        }
    

    您是否愿意使用外部库,如PHP Simple HTML DOM Parser()?这会让你的生活变得非常简单。请看@xbonez SimpleHTMLDOM。理想情况下,只需几个函数就可以更好地保留它light@xbonezlibxml有一个用于html的解析器模块,允许解析损坏的html。我不确定是否可以从XMLReader访问,但它是从DOM访问的。DOM需要比XMLReader更多的内存,因为它将XML解析为内存中的树结构。不过,它应该比SimpleHtmlDom更快,占用的内存更少。在您的下一个项目中尝试一下:)您误读了文章-应该对PHP数组执行相反的无序列表
    //output a multi-dimensional array as a nested UL
    function toUL($array){
        //start the UL
        echo "<ul>\n";
           //loop through the array
        foreach($array as $key => $member){
            //check for value member
            if(isset($member['value']) ){
                //if value is present, echo it in an li
                echo "<li>{$member['value']}</li>\n";
            }
            else if(is_array($member)){
                //if the member is another array, start a fresh li
                echo "<li>\n";
                //and pass the member back to this function to start a new ul
                toUL($member);
                //then close the li
                echo "</li>\n";
            }
        }
        //finally close the ul
        echo "</ul>\n";
    }
    
    function ul_to_array($ul){
            if(is_string($ul)){
                if(!$ul = simplexml_load_string($ul)) {
                    trigger_error("Syntax error in UL/LI structure");
                    return FALSE;
                }
                return ul_to_array($ul);
            } else if(is_object($ul)){
                $output = array();
                foreach($ul->li as $li){
                    $update_with = (isset($li->ul)) ? ul_to_array($li->ul) : (($li->count()) ? $li->children()->asXML() : (string) $li);
                    if(is_string($update_with)){
                        if(trim($update_with) !== "" && $update_with !== null){
                            $output[] = $update_with;
                        }
                    } else {
                            $output[] = $update_with;
                    }
                }
                return $output;
            } else {
                return FALSE;
            }
        }