简体   繁体   English

如何使用php将树状数组输出到列表中?

[英]How do I output an tree-like array into a list with php?

My main problem is that I want to generate an unordered list in HTML using PHP. 我的主要问题是我想使用PHP在HTML中生成无序列表。 I get the data from a SQL query which then needs to be concated into an unordered list using a PHP function I tried to write myself. 我从SQL查询中获取数据,然后需要使用自己尝试编写的PHP函数将其浓缩为无序列表。 Every item should be a li and if it has a subitem, it should open a new ul with the subitems as li which again can contain subitems, etc. 每个项目都应该是li ,如果有子项目,则应使用li子项目打开一个新的ul ,它又可以包含子项目,依此类推。

I have an array where I output every part of a tree itself. 我有一个数组,我在其中输出树本身的每个部分。 It looks like this: 看起来像这样:

[0] => Array (
    [0] => Application Integration
    [1] => 
    )
[1] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => 
    )
[2] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => Leitungen
    )
[3] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => Leitungen
    [3] => WAN
    )
[4] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => Leitungen
    [3] => Mail
    )
[5] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => EDI
    )
[6] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => EDI
    [3] => Word
    )
[7] => Array (
    [0] => Application Integration
    [1] => Windows
    [2] => EDI
    [3] => LAN
    )
[8] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => 
    )
[9] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Office
    )
[10] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Office
    [3] => Powerpoint
    )
[11] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Office
    [3] => Excel
    )
[12] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Leitungen
    )
[13] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Leitungen
    [3] => SQL Developer
    )
[14] => Array (
    [0] => Application Integration
    [1] => Internet
    [2] => Leitungen
    [3] => Pokerstars
    )

I want the output to be surrounded by an unordered list where every item in the array is a list element and if it has a subitem it should also be in an unordered list, ... It should look like the following list: 我希望输出被一个无序列表包围,其中数组中的每个项目都是一个列表元素,如果它有一个子项,它也应该位于无序列表中,...看起来应该像下面的列表:

<ul>
  <li>
    Applicaton Integration
    <ul>
      <li>
        Windows 
        <ul>
          <li>
            EDI
            <ul>
              <li>Word</li>
              <li>LAN</li>
            </ul>
          </li>
        </ul>
      </li>
      <li>
        Internet
        <ul>
          <li></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>
...etc

I tried to write the following function in PHP but it is not quite what i wanted since it closes the tags to early and/or too late. 我试图用PHP编写以下函数,但这并不是我想要的,因为它会使标记过早和/或太晚关闭。

function treeOut(array $tree): string{
  $markup = '';
  print_r($tree);
  foreach($tree as $branch) {
    if(!empty($branch[1])){
      $markup.='<ul class="Stufe1">';
      $markup.='<li>';
        if(!empty($branch[2])){
          $markup.='<ul class="Stufe2">';
          $markup.='<li>';
            if(!empty($branch[3])){
              $markup.='<ul class="Stufe3">';
              $markup.='<li>';
              $markup.='<input type="checkbox" name=""/>'.$branch[3];
              $markup.='</li></ul>';
            }else{
              $markup.='<input type="checkbox" name=""/>'.$branch[2];
            }
            $markup.='</ul></li>';
          }else{
          $markup.='<input type="checkbox" name=""/>'.$branch[1];
        }
        $markup.='</li></ul>';
    }else{
      $markup.='<li><input type="checkbox" name=""/>'.$branch[0];
    }
  }
  return $markup;
}

I am sorry for that much code but I would appreciate any help. 对于这么多的代码,我感到很抱歉,但我将不胜感激。

To solve a problem involving nested levels of data structures that should however be treated roughly the same way, recursivity is your best friend. 要解决涉及嵌套数据结构级别的问题,但是应该以大致相同的方式对待它,递归是您最好的朋友。

In case you aren't familiar with this technique, the idea is to design a function that will call itself. 如果您不熟悉此技术,则其想法是设计一个可以自我调用的函数。

Regarding your problem, we can consider something like this : 关于您的问题,我们可以考虑这样的事情:

First, a function to modify your array so that it can be treated. 首先,一个修改数组的函数,以便可以对其进行处理。

function nester(array $array, $idx = 0) {
    if (array_key_exists($idx, $array) && !empty($array[$idx])) {
        return array($array[$idx] => nester($array, $idx + 1));
    }
    return array();
}

function treeParser(array $array_tree) {
    $dom_tree = '';
    foreach ($array_tree as $key => $val) {
        if (is_array($val) && count($val)) {
            $dom_tree .= "<li>$key".treeParser($val).'</li>';
        } else {
            $dom_tree .= "<li>$key</li>";
        }
    }
    return ($dom_tree ? "<ul>$dom_tree</ul>" : '');
}

$array_nested = array();

foreach ($array as $value) {
    $array_nested[] = nester($value);
}
$DOM_tree = treeParser(array_merge_recursive(... $array_nested));

Beforehand, a function browses each of your subarrays and convert them to nested arrays so tht we can recurse on them. 事先,函数会浏览您的每个子数组并将其转换为嵌套数组,以便我们可以对其进行递归。 With some help from array_merge_recursive , these arrays are merged into a clean tree of the DOM you seek to build. array_merge_recursive帮助下,这些数组被合并到您要构建的DOM的干净树中。

The other function then takes your array as a parameter and browses it with a foreach. 然后,另一个函数将您的数组作为参数,并使用foreach浏览它。 Each time it finds an array, it appends a pair of <li> tags containing the current array's key, and eventually the return of itself called on that array provided it isn't empty. 每次找到一个数组时,它都会附加一对<li>标记,其中包含当前数组的键,如果该数组不为空,则最终在该数组上调用本身的返回。 When an instance of the function has treated its whole subarray, it returns its work - the string $dom_tree - between <ul> tags. 函数的实例处理完整个子$dom_tree ,将返回其工作- <ul>标记之间的字符串$dom_tree If that instance was the main one - the one you called yourself - these <ul> tags will be the main unordered list containing your whole data structure. 如果该实例是主要实例-您称为自己的实例-这些<ul>标记将成为包含整个数据结构的主要无序列表。

It theorically works whatever the depth of your array is, but beware the Stack Overflow if it's insanely deep. 从理论上讲,无论数组的深度如何,它都可以工作,但是请注意,如果堆栈深度过深,则应注意堆栈溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM