PHP:如何生成
  • xml2assoc 数组结果中的树?

发布于 2024-09-01 02:07:48 字数 1364 浏览 1 评论 0原文

我见过很多关于如何生成

  • 标记的 PHP 函数,但我猜我的数组输入相当复杂。 数组

它是从名为 xml2assoc 的自定义函数返回的 问题是如何使用 PHP 将返回的 xml2assoc 数组结果转换为

  • 格式的 HTML 代码。

谢谢。

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

我需要生成的最终输出是这样的:

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

    <li>Module 1</li>
</ul>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

靑春怀旧 2024-09-08 02:07:48

如果您有 XML 作为输入,为什么不使用 XSLT 将其转换为

我猜你的输入看起来像这样(我假设“NavigationMode”是一个拼写错误):

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

使用一个小的 XSLT 1.0 样式表:

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

生成此输出:

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

PHP 文档展示了如何使用 XSLT。 很简单。

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.

水晶透心 2024-09-08 02:07:48

下面是一个针对数组结构的快速 PHP 实现,可以帮助您入门:

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

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>';
}
如梦亦如幻 2024-09-08 02:07:48

我没有测试它的演示数据的变化......

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

?>

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文