如何在输出中包含子元素的每个子元素?PHP


How to include every child element of a child element in output? PHP

我有一个小应用程序,它可以执行以下操作:

  • 允许用户上载XML文件
  • 将XML文件解析为数组以使用$_SESSION
  • 显示用户可以选择查看的父元素名称列表
  • 解析数组以显示所选父元素的子元素

问题是,每个项目都可以有可以有孩子的孩子。。。等等…而且这种情况可能会无限期地持续下去。

如何在最终输出中包含子级的每个子级

XML文件可以类似于以下内容:

<thing>
  <parents>
    <parent>
      <name>parent 1</name>
      <categories>
        <category>
          <name>category 1</name>
          <items>
            <item>
              <name>item 1 (gen 1)</name>
              <items>
                <item>
                  <name>sub-item 1 (gen 2)</name>
                  <items>
                    <item>
                      <name>sub-sub-item 1 (gen 3)</name>
                      ...this could continue forever..
                    </item>
                  </items>
                </item>
              </items>
            </item>
          </items>
        </category>
      </categories>
    </parent>
  </parents>
</thing>

我已经使用PHP的SimpleXML将XML解析为一个数组。每个文件都必须有一个父项、类别和第一代子项。下面的代码解析了这三个层次的结构,但除此之外,我就迷失了方向。

$output .= '<ul>';
foreach($xml['parents'] as $parent){
  $output .= '<li>'.$parent['name'].'</li>';
  foreach($parent['categories']['category'] as $category){
    $output .= '<li>'.$category['name'].'</li>';
    foreach($category['items']['item'] as $item){
      $output .= '<li>'.$item['name'].'</li>';
      // here is where the $item can have children w/ children
      // who can have children who can have children... etc... forever.
      // (i.e. $item['items']['item'] as $generation2_items++...)
      //
      // I want to add them as another <li></li>...
      //
      // How can you account for unknown # of children?
    }
  }
}
$output .= '</ul>';
echo $output;

代码$输出的列表类似于:

- parent 1
-- category 1
--- item 1 (gen 1)
---- sub item 1 (gen 2)
----- sub-sub item 1 (gen 3)
------- etc.
-------- etc.

如何确定每个项目的子元素深度,然后如何创建足够的循环来进行相应的解析。。。还是通过另一种方式迭代?

谢谢你的帮助。

解决方案

PHP递归函数解决了这个问题

function foo($element, $indent=0){
  $result .= '<li>';
  if($indent > 0){
    for($i=1;$i<=$indent;$i++){
      $result .= '&nbsp;&nbsp;&nbsp;&nbsp;';
    }
  }
  $result .= $element['name'].'</li>';
  if(isset($element['children']['child'])){
    $i++;
    foreach($element['children']['child'] as $child){
   $result .= foo($child, $i);
    }
  }
  return $result;
}    
$output .= foo($item);

您可以为此使用递归函数。每个程序员都应该知道如何使用递归;如果你不这样做:马上去学吧!

您基本上想要做的是创建一个函数,让我们称之为foo(),它将一个项作为输入。foo将做两件事:

  1. 输出当前项目
  2. 对于每个子级,使用该子级作为输入调用自身

正如我所说,创建递归函数非常有用,您应该学习和练习这个工具。例如,您可以向foo发送具有递归深度的第二个参数,这样您就可以输出具有不同缩进的子级。

在伪代码中,递归节点遍历函数可能如下所示:

function traverse(node)
{
    print(node);
    if(node.hasChildren()) {
        foreach(node.children as child) {
            traverse(child);
        }
    }
}

希望能有所帮助!:)