无递归地从多维数组构建无序列表


Build unordered list from multidimensional array without recursion

我在从包含实体及其子实体的多维数组构建无序列表时遇到问题。问题是我不想使用递归,因为树可能会变得非常深,递归可能会在服务器上产生不必要的负载。

这是这样一个数组的例子(它被简化为只包含titlechildren,实体也可以是对象)。

$array = array(
    array('title' => '1', 'children' => array()),
    array('title' => '2', 'children' => array()),
    array('title' => '3', 'children' => array()),
    array('title' => '4', 'children' => array(
        array('title' => '41', 'children' => array()),
        array('title' => '42', 'children' => array()),
        array('title' => '43', 'children' => array()),
        array('title' => '44', 'children' => array(
            array('title' => '441', 'children' => array()),
            array('title' => '442', 'children' => array()),
            array('title' => '443', 'children' => array()),
            array('title' => '444', 'children' => array(
                array('title' => '4441', 'children' => array()),
                array('title' => '4442', 'children' => array()),
                array('title' => '4443', 'children' => array())
            )),
        )),
        array('title' => '45', 'children' => array())
    )),
    array('title' => '5', 'children' => array()),
    array('title' => '6', 'children' => array(
        array('title' => '61', 'children' => array()),
        array('title' => '62', 'children' => array()),
        array('title' => '63', 'children' => array())
    )),
    array('title' => '7', 'children' => array())
);

在这里做了一些研究,所以我想出了这个解决方案,非常接近我想要的:

<html>
<head></head>
<body>
<ul>
<?php
$stack = $array;
$i = 0;
$counts = array();
while(!empty($stack)) {
    $node = array_shift($stack);
    echo "<li>{$node['title']}";
    if($node['children']) {
        echo "<ul>";
        $counts[] = count($node['children']);
        $node['children'] = array_reverse($node['children']);
        foreach($node['children'] as $ch)
            array_unshift($stack, $ch);
    }
    if(!empty($counts)) {
        end($counts);
        if($counts[$key] == 0) {
            echo "</ul>";
            array_pop($counts);
        } else {
            $counts[$key]--;
        }
    }
    if(!$node['children']) {
        echo "</li>";
    }
    // just to make sure we won't end in infinite loop
    $i++;
    if($i == 50) break;
}
?>
</ul>
</body>
</html>

输出如下-正如您所看到的,我的问题只是子树的关闭</ul>我的问题:是我想多了,还是我瞎了,没有看到一个明显的错误?你能把我推向有限的解决方案还是给我你自己的解决方案?

输出:

    1
  • 2
  • 3
  • 4
      41
    • 42
    • 43
    • 44
      • 441
      • 442
      • 443
      • 444
        • 4441
        • 4442
        • 4443
      • 45
    • 5
    • 6
      • 61
      • 62
      • 63
  • 7

这不是对您的代码的修复,但也许它会对您有所帮助:

function helper($input) {
  $input[] = '</ul>';
  $input = array_reverse($input);
  $input[] = '<ul>';
  // output
  while (sizeof($input) > 0) {
    $el = array_pop($input);
    if (!is_array($el)) {
      echo $el; 
    }
    else {
      // add current element
      $input[] = sprintf('<li>%s', $el['title']);
      // add children
      if (sizeof($el['children']) > 0) {
        $input[] = '</ul>';
        $input = array_merge($input, array_reverse($el['children']));
        $input[] = '<ul>';
      }
      // add closing li
      $input[] = '</li>';
    }
  }
}
helper($array);

演示:http://codepad.viper - 7. - com/qin15v

好吧,就像往常一样- ask for a solution You are solving for hours and immediately You are out with one... -所以我在稍微放松一下之后又思考了一下,想出了这个解决方案:

<html>
<head></head>
<body>
<ul>
<?php
$i = 0;
$counts = array();
while(!empty($stack)) {
    $node = array_shift($stack);
    if(!empty($counts)) {
        while(end($counts) === 0) {
            $key = key($counts);
            echo "</li></ul>";
            array_pop($counts);
        }
        end($counts);
        $key = key($counts);
        if(isset($counts[$key])) {
            $counts[$key]--;
        }
    }
    echo "<li>{$node['title']}";
    if($node['children']) {
        echo "<ul>";
        $counts[] = count($node['children']);
        $node['children'] = array_reverse($node['children']);
        foreach($node['children'] as $ch)
            array_unshift($stack, $ch);
    }
    if(!$node['children']) {
        echo "</li>";
    }

    $i++;
    if($i == 50) break;
}
?>
</ul>
<p>$i = <?php echo $i; ?></p>
</body>
</html>

问题是我错过了另一个while,应该减去$counts的值一路回到树…

我会等待接受我自己的答案,以防有人会发布他们更好的解决方案,而不是我的不那么整洁的解决方案。