我使用的是jsTree,我需要将这个HTML树代码<ul> <li>转换成一个PHP数组。PHP标记将传递给PHP进行解析,并存储在一个结构化树jsTree数组中(请参阅下面的PHP数组结构)。
附加问题:我想要的PHP数组结构是好的还是可以建议一个好的结构?我愿意听取你的建议。
(预先谢谢:)
干杯,马克
jsTree截图

HTML树字符串
<ul class="ltr">
<li id="phtml_1" class=" open">
<a style="" class=" " href="#"><ins> </ins>Folder 1</a>
<ul>
<li class="leaf" id="phtml_2">
<a style="" class=" " href="#"><ins> </ins>Child 1.1</a>
</li>
<li class="open" id="phtml_3">
<a style="" class=" " href="#"><ins> </ins>Folder 1.1</a>
<ul>
<li class="leaf last" rel="default">
<a href="" style="" class=" "><ins> </ins>Child 1.1.1</a>
</li>
</ul>
</li>
<li class="last open" rel="default">
<a href="" style="" class=" "><ins> </ins>Folder 1.2</a>
<ul>
<li class="leaf" rel="default">
<a href="" style="" class=" "><ins> </ins>Child 1.2.1</a>
</li>
<li class="leaf last" rel="default">
<a href="" style="" class=" "><ins> </ins>Child 1.2.2</a>
</li>
</ul>
</li>
</ul>
</li>
<li id="phtml_5" class="file open">
<a style="" class=" " href="#"><ins> </ins>Folder 2</a>
<ul>
<li class="leaf" rel="default">
<a href="" style="" class=" "><ins> </ins>Child 2.1</a>
</li>
<li class="leaf last" rel="default">
<a href="" style="" class="clicked"><ins> </ins>Child 2.2</a>
</li>
</ul>
</li>
<li class="leaf last" rel="default">
<a href="" style="" class=" "><ins> </ins>Outer Child</a>
</li>
</ul>PHP结构
<?php
$tree_array = array(
'Folder 1' => array(
'Child 1.1',
'Folder 1.1' => array(
'Child 1.1.1'
),
'Folder 1.2' => array(
'Child 1.2.1',
'Child 1.2.2'
),
),
'Folder 2' => array(
'Child 2.1',
'Child 2.2'
),
'Outer Child'
);
echo '<pre>',print_r($tree_array),'</pre>';
?>print_r输出
Array
(
[Folder 1] => Array
(
[0] => Child 1.1
[Folder 1.1] => Array
(
[0] => Child 1.1.1
)
[Folder 1.2] => Array
(
[0] => Child 1.2.1
[1] => Child 1.2.2
)
)
[Folder 2] => Array
(
[0] => Child 2.1
[1] => Child 2.2
)
[0] => Outer Child
)发布于 2011-01-20 09:49:50
仅供参考:还有一个jQuery插件,用于序列化HTML of和lists:http://www.drakedata.com/serializetree/sampleTree.html --它将嵌套的结构和列表转换为字符串,该字符串可用作关联的php数组。
https://stackoverflow.com/questions/2817242
复制相似问题