PHP:如何生成- xml2assoc 数组结果中的树?
我见过很多关于如何生成
标记的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有 XML 作为输入,为什么不使用 XSLT 将其转换为
?
我猜你的输入看起来像这样(我假设“NavigationMode”是一个拼写错误):
使用一个小的 XSLT 1.0 样式表:
生成此输出:
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):
With a small XSLT 1.0 stylesheet:
This output is produced:
The PHP documentation shows how to use XSLT. It's simple.
下面是一个针对数组结构的快速 PHP 实现,可以帮助您入门:
Here is a quick PHP implementation for your array structure to get you started:
我没有测试它的演示数据的变化......
i didn't test it for variations of the demo data ...