从多维数组创建嵌套列表 [英] Create nested list from Multidimensional Array
问题描述
我在 PHP 中有一个数组,如下所示:
数组 ([0] =>大批 ([id] =>1[标题] =>标题1"[parent_id] =>空值[深度] =>0)[1] =>大批 ([id] =>2[标题] =>标题2"[parent_id] =>空值[深度] =>0)[2] =>大批 ([id] =>3[标题] =>标题3"[parent_id] =>2[深度] =>1)[3] =>大批 ([id] =>4[标题] =>标题4"[parent_id] =>2[深度] =>1)[4] =>大批 ([id] =>5[标题] =>标题5"[parent_id] =>空值[深度] =>0)[5] =>大批 ([id] =>6[标题] =>标题6"[parent_id] =>4[深度] =>2))
我想要做的是迭代这个数组并从中创建一个嵌套的
- 列表.所以结果应该是这样的:
- ".$i['title'] .r( $a, $i['id'] ) ."</li>";}}$r = $r ."</ol>";返回 $r;}打印 r( $a, $level );?>
- Title 1
- Title 2
- Title 3
- </ol></li><li>Title 4<ol><li>Title 6<ol></ol></li></ol></li>/ol></li><li>标题 5<ol></ol></li></ol>
- Title 3
- 标题 1\n
- 标题 2\n
- 标题 3\n
- Title 4\n
- Title 6\n
- Title 6\n
- 标题 3\n
- Title 5\n
- ".$i['title'] .r( $a, $i['id'] ) ."</li>";}}return ($r==''?'':"<ol>". $r ."</ol>");}
- Title 1\n
- Title 2\n
- Title 3\n
- Title 4\n
- Title 6\n
- Title 6\n
- Title 3\n
- Title 5\n
<li>标题 1</li>//id = 1<li>标题2</li>//id = 2<ol><li>标题3</li>//id = 3 ->parent_id = 2<li>标题4</li>//id = 4 ->parent_id = 2<ol><li>标题6</li>//id = 6 ->parent_id = 4</ol></ol><li>标题5</li>//id = 5</ol>
我一直在想办法如何完成这项工作.但到目前为止,每一次尝试都失败了......
有谁知道我如何从这样的数组中创建这样一个嵌套的 <ol>
列表?
请注意,我对给定的数据没有任何控制权.我只是调用一个 API,它返回 json 数据,我将其转换为数组.这个数组看起来和我描述的一模一样.
你应该使用递归:
首先是'php'语法中的数组:
大批 ('id' =>1、'标题' =>标题1",'parent_id' =>'空值','深度' =>0),'1' =>大批 ('id' =>2、'标题' =>标题2",'parent_id' =>'空值','深度' =>0),'2' =>大批 ('id' =>3、'标题' =>标题3",'parent_id' =>2、'深度' =>1),'3' =>大批 ('id' =>4、'标题' =>标题4",'parent_id' =>2、'深度' =>1),'4' =>大批 ('id' =>5、'标题' =>标题5",'parent_id' =>'空值','深度' =>0),'5' =>大批 ('id' =>6、'标题' =>标题6",'parent_id' =>4、'深度' =>0));
这里是代码:
$level = 'NULL';函数 r( $a, $level) {$r = "";foreach ( $a 作为 $i ) {如果 ($i['parent_id'] == $level ) {$r = $r .
结果:
在检查为解决方案后进行编辑
避免空叶子:
function r( $a, $level) {$r = '' ;foreach ( $a 作为 $i ) {如果 ($i['parent_id'] == $level ) {$r = $r .
I have an array in PHP, which looks like this:
array (
[0] => array (
[id] => 1
[title] => "Title 1"
[parent_id] => NULL
[depth] => 0
)
[1] => array (
[id] => 2
[title] => "Title 2"
[parent_id] => NULL
[depth] => 0
)
[2] => array (
[id] => 3
[title] => "Title 3"
[parent_id] => 2
[depth] => 1
)
[3] => array (
[id] => 4
[title] => "Title 4"
[parent_id] => 2
[depth] => 1
)
[4] => array (
[id] => 5
[title] => "Title 5"
[parent_id] => NULL
[depth] => 0
)
[5] => array (
[id] => 6
[title] => "Title 6"
[parent_id] => 4
[depth] => 2
)
)
What i want to do is iterate over this array and create a nested <ol>
list from it. So the result should look like this:
<ol>
<li>Title 1</li> // id = 1
<li>Title 2</li> // id = 2
<ol>
<li>Title 3</li> // id = 3 -> parent_id = 2
<li>Title 4</li> // id = 4 -> parent_id = 2
<ol>
<li>Title 6</li> // id = 6 -> parent_id = 4
</ol>
</ol>
<li>Title 5</li> // id = 5
</ol>
I've been trying to think of a way how i could get this done. But so far every attempt failed...
Anyone any idea how i can create such a nested <ol>
list from an array like that?
Please note that i do not have any control on the given data. I simply make a call to an API and it returns json data, which i convert to an array. And the array looks exactly like the one i described.
You should use recursion:
First the array in 'php' syntax:
<?php
$a=array (
'0' => array (
'id' => 1,
'title' => "Title 1",
'parent_id' => 'NULL',
'depth' => 0
),
'1' => array (
'id' => 2,
'title' => "Title 2",
'parent_id' => 'NULL',
'depth' => 0
),
'2' => array (
'id' => 3,
'title' => "Title 3",
'parent_id' => 2,
'depth' => 1
),
'3' => array (
'id' => 4,
'title' => "Title 4",
'parent_id' => 2,
'depth' => 1
),
'4' => array (
'id' => 5,
'title' => "Title 5",
'parent_id' => 'NULL',
'depth' => 0
),
'5' => array (
'id' => 6,
'title' => "Title 6",
'parent_id' => 4,
'depth' => 0
)
);
Here the code:
$level = 'NULL';
function r( $a, $level) {
$r = "<ol>";
foreach ( $a as $i ) {
if ($i['parent_id'] == $level ) {
$r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
}
}
$r = $r . "</ol>";
return $r;
}
print r( $a, $level );
?>
The results:
<ol><li>Title 1<ol></ol></li><li>Title 2<ol><li>Title 3<ol>
</ol></li><li>Title 4<ol><li>Title 6<ol></ol></li></ol></li></ol></li><li>Title 5
<ol></ol></li></ol>
EDITED AFTER CHECK AS SOLUTION
To avoid empty leafs:
function r( $a, $level) {
$r = '' ;
foreach ( $a as $i ) {
if ($i['parent_id'] == $level ) {
$r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
}
}
return ($r==''?'':"<ol>". $r . "</ol>");
}
这篇关于从多维数组创建嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!