Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用分层数据结构打印列表?_Php_List_Hierarchy - Fatal编程技术网

Php 如何使用分层数据结构打印列表?

Php 如何使用分层数据结构打印列表?,php,list,hierarchy,Php,List,Hierarchy,当我运行此代码时: foreach ($tree as $node) { echo str_repeat(' ', $node->tree_depth * 4) . $node->id . PHP_EOL; } 我得到格式良好的文本,如: Food Fruit Red Cherry Strawberry Cool Not cool Yellow Banana Meat

当我运行此代码时:

foreach ($tree as $node) {
    echo str_repeat(' ', $node->tree_depth * 4) . $node->id . PHP_EOL;
}
我得到格式良好的文本,如:

Food
 Fruit
   Red
     Cherry
     Strawberry
               Cool
               Not cool
   Yellow
     Banana
 Meat
   Beef
   Pork
但是我想用
  • 创建一个列表

    我试过:

    echo '<ul>';
    $prev_depth = 0;
    foreach($table->fetchTree() as $row) {
        if ($row->tree_depth > $prev_depth) {
            echo '<li><ul>';
        } else if ($row->tree_depth < $prev_depth) {
            echo '</li></ul>';
        }
        echo '<li>' . $row->name . '</li>';
        $prev_depth = $row->tree_depth;
    }
    echo '</ul>';
    
    echo'
      '; $prev_depth=0; foreach($table->fetchTree()作为$row){ 如果($row->tree\u depth>$prev\u depth){ 回音“
      • ”; }else if($row->tree_depth<$prev_depth){ 回音“
      ”; } 回显“
    • ”.$row->name.“
    • ”; $prev_depth=$row->tree_depth; } 回声“
    ”;
    但是我有一些额外的ul标签等等。我为此耽搁了2天,因此如果您能帮助我,请在此处发布…

    请尝试以下代码:

    <?php
    echo "<ul>\n";
    
    $tree = array(
        array('Food', 0),
        array('Fruit', 1),
        array('Red', 5),
        array('Cherry', 3),
        array('Strawberry', 3),
        array('Cool', 4),
        array('Not cool', 4),
        array('Yellow', 2),
        array('Banana', 3),
        array('Meat', 0),
        array('Beef', 4),
        array('Pork', 2),
    );
    
    $depth = 0;
    
    foreach ($tree as $node) {
      if ($node[1] > $depth)
        echo str_repeat("<ul>\n", $node[1] - $depth);
      if ($node[1] < $depth)
        echo str_repeat("</ul>\n", $depth - $node[1]);
      $depth = $node[1];
    
      echo "<li>" .  $node[0] . "\n";
    }
    echo str_repeat("</ul>\n", $depth+1);
    ?>
    
    
    
    我已经更新了它,以输出更少的
  • 标记,从而减少了项目符号的数量。但另一方面,这将生成无法验证的HTML,因为超过一个级别的跳转将导致生成

        尝试以下算法:

        $tree = array(
            array('Food', 0),
            array('Fruit', 1),
            array('Red', 2),
            array('Cherry', 3),
            array('Strawberry', 3),
            array('Cool', 4),
            array('Not cool', 4),
            array('Yellow', 2),
            array('Banana', 3),
            array('Meat', 0),
            array('Beef', 1),
            array('Pork', 1),
        );
        
        $depth = -1;
        $flag = false;
        foreach ($tree as $row) {
            while ($row[1] > $depth) {
                echo "<ul>\n", "<li>";
                $flag = false;
                $depth++;
            }
            while ($row[1] < $depth) {
                echo "</li>\n", "</ul>\n";
                $depth--;
            }
            if ($flag) {
                echo "</li>\n", "<li>";
                $flag = false;
            }
            echo $row[0];
            $flag = true;
        }
        while ($depth-- > -1) {
            echo "</li>\n", "</ul>\n";
        }
        
        $tree=数组(
        数组('Food',0),
        数组('水果',1),
        数组('Red',2),
        数组('Cherry',3),
        数组('草莓',3),
        阵列('Cool',4),
        数组('不酷',4),
        数组('黄色',2),
        数组('Banana',3),
        数组('Meat',0),
        数组('Beef',1),
        数组('Pork',1),
        );
        $depth=-1;
        $flag=false;
        foreach($tree作为$row){
        while($row[1]>$depth){
        回声“
          \n”、“
        • ”; $flag=false; $depth++; } 而($row[1]<$depth){ 回声“
        • \n”和“
        \n”; $depth--; } 如果($flag){ 回声“\n”,“
      • ”; $flag=false; } echo$行[0]; $flag=true; } 而($depth-->-1){ 回声“
      • \n”和“
      \n”; }

    在这里,您只需将
    $tree
    替换为
    $table->fetchTree()
    $row[0]
    替换为
    $row->name
    $row[1]
    替换为
    $row->tree\u depth

    我们可能需要一个小数据集,所以这不是一个解决方案…很难看出你真正想要的是什么。。。我想,我的问题可能不是很清楚,但我发布的代码非常清楚:D无论如何,谢谢你的帮助,我很感激,真的……太好了,我很高兴听到你这么说!Gumbo,信不信由你,我发现你的算法有问题,但不幸的是,我无法解决它,问题是你的算法没有生成有效的ul li结构,并且你有一些额外的不必要的标记,你能解决它吗!?我没有看到任何错误。你有一个输出无效的例子吗?是的,我有,你想让我怎么发送它!?如果你愿意,我也可以给你发电子邮件。这是输出,它在浏览器中看起来正常,但如果您分析源代码,您将看到问题出在代码中的某个地方,而不是
。但是我的是正确的。那么这行的
是从哪里来的呢?看见