PHP: How to generate a
  • tree in an xml2assoc array result?

后端 未结 3 779
名媛妹妹
名媛妹妹 2021-01-16 00:06

I have seen many PHP function on how to generate a

  • tag but my array input is quite complicated I guess. It is an array returned from a cust
相关标签:
3条回答
  • 2021-01-16 00:40

    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.

    0 讨论(0)
  • 2021-01-16 00:43

    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>';
    }
    
    0 讨论(0)
  • 2021-01-16 00:45

    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>';
    
    ?>
    
    0 讨论(0)
提交回复
热议问题