PHP: How to generate a <ul><li> tree in an xml2assoc array result?

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I have seen many PHP function on how to generate a <ul><li> tag but my array input is quite complicated I guess. It is an array returned from a custom function called xml2assoc

My question is how can I convert the returned xml2assoc array result to a <ul><li> formatted HTML code using PHP.

Thanks.

$tree = array(     0 => array(         'tag' => 'NavigationMode',         'value' => array(             0 => array(                 'tag' => 'Title',                 'value' => 'Introduction'             ),             1 => array(                 'tag' => 'NavigationNode',                 'value' => array(                     0 => array(                         'tag' => 'Title',                         'value' => 'Sub Intro'                     )                 )             )         )     ),     1 => array(         'tag' => 'NavigationMode',         'value' => array(             0 => array(                 'tag' => 'Title',                 'value' => 'Module 1'             )         )     ) ); 

The final output that I need to generate is like this:

<ul>     <li>     Introduction     <ul>         <li>Sub Intro</li>     </ul>     </li>      <li>Module 1</li> </ul> 

回答1:

i didn't test it for variations of the demo data ...

<?php  function getTitle($node) {     foreach ($node['value'] as $cnode) {             if ($cnode['tag'] == 'Title') {                 return $cnode['value'];             }     }      return 'untitled'; }  function getCNodes($node) {     $cnodes = array();      foreach ($node['value'] as $cnode) {         if ($cnode['tag'] == 'NavigationNode') {             $cnodes[] = $cnode;         }     }      return $cnodes; }  function runTree($node) {     $title  = getTitle($node);     $cnodes = getCNodes($node);      if (count($cnodes) > 0) {         $out = '<li>' . $title . "\n" . '<ul>';         foreach ($cnodes as $cnode) {             $out .= runTree($cnode);         }         $out .= '</ul>' . "\n" . '</li>' . "\n";          return $out;     } else {         return '<li>' . $title . '</li>' . "\n";     } }   $tree = array(     0 => array(         'tag' => 'NavigationMode',         'value' => array(                 0 => array(                         'tag' => 'Title',                         'value' => 'Introduction'                 ),                 1 => array(                         'tag' => 'NavigationNode',                         'value' => array(                                 0 => array(                                         'tag' => 'Title',                                         'value' => 'Sub Intro'                                 )                         )                 )         )     ),     1 => array(         'tag' => 'NavigationMode',         'value' => array(                 0 => array(                         'tag' => 'Title',                         'value' => 'Module 1'                 )         )     ) );    echo '<ul>'; foreach ($tree as $node) {     echo runTree($node); } echo '</ul>';  ?> 


回答2:

If you have XML as input, why not use XSLT to transform it to <ul>?

I guess your input looks something like this (I assume "NavigationMode" is a typo):

<tree>   <NavigationNode>     <title>Introduction</title>     <NavigationNode>       <title>Sub Intro</title>     </NavigationNode>   </NavigationNode>   <NavigationNode>     <title>Module 1</title>   </NavigationNode> </tree> 

With a small XSLT 1.0 stylesheet:

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    <xsl:output omit-xml-declaration="yes" indent="yes" />    <xsl:template match="/tree">     <ul>       <xsl:apply-templates select="NavigationNode" />     </ul>   </xsl:template>    <xsl:template match="NavigationNode">     <li>       <xsl:value-of select="title" />       <xsl:if test="NavigationNode">         <ul>           <xsl:apply-templates select="NavigationNode" />         </ul>       </xsl:if>     </li>   </xsl:template>  </xsl:stylesheet> 

This output is produced:

<ul>   <li>     Introduction     <ul>       <li>Sub Intro</li>     </ul>   </li>   <li>Module 1</li> </ul> 

The PHP documentation shows how to use XSLT. It's simple.



回答3:

Here is a quick PHP implementation for your array structure to get you started:

function create_html_list($nodes) {     echo '<ul>';      foreach ($nodes as $node) {         $childNodes = $node['value'];         $titleNode = array_shift($childNodes);          echo "<li>", $titleNode['value'];          if (count($childNodes) > 0) {             create_html_list($childNodes);         }          echo "</li>";     }      echo '</ul>'; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!