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

275

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>
823

Answer

Solution:

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.

333

Answer

Solution:

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>';
}
500

Answer

Solution:

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>';

?>

People are also looking for solutions to the problem: MySql PHP select count of distinct values from comma separated data (tags)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.