Php 从多维数组创建嵌套列表
我在PHP中有一个数组,如下所示:Php 从多维数组创建嵌套列表,php,arrays,Php,Arrays,我在PHP中有一个数组,如下所示: array ( [0] => array ( [id] => 1 [title] => "Title 1" [parent_id] => NULL [depth] => 0 ) [1] => array ( [id] => 2 [title] => "Title 2" [pare
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
)
)
<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>
我要做的是迭代这个数组并从中创建一个嵌套列表。所以结果应该是这样的:
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
)
)
<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>
我一直在想一个办法,怎样才能完成这件事。但到目前为止,每一次尝试都失败了
有人知道如何从这样的数组中创建这样的嵌套列表吗
请注意,我对给定的数据没有任何控制权。我只需调用一个API,它就会返回json数据,并将其转换为数组。数组看起来与我描述的完全一样。您应该使用递归:
首先使用“php”语法创建数组:
<?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
)
);
代码如下:
结果是:
标题1\n标题2\n标题3\n标题4\n标题6\n标题5\n
检查后编辑为解决方案
要避免空树叶:
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>");
}
你可以试试下面的方法
$array = 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
)
);
echo(make($array));
输出
<ol>
<li>Title 1</li>
<li>Title 2</li>
<ol>
<li>Title 3</li>
<li>Title 4</li>
<ol>
<li>Title 6</li>
</ol>
</ol>
<li>Title 5</li>
</ol>
使用的功能
function make(array $array, $no = 0) {
$child = hasChildren($array, $no);
if (empty($child))
return "";
$content = "<ol>\n";
foreach ( $child as $value ) {
$content .= sprintf("\t<li>%s</li>\n", $value['title']);
$content .= make($array, $value['id']);
}
$content .= "</ol>\n";
return $content;
}
function hasChildren($array, $id) {
return array_filter($array, function ($var) use($id) {
return $var['parent_id'] == $id;
});
}
以下阵列:
Array
(
[0] => Content
[1] => Array
(
[0] => International
[1] => Array
(
[0] => Mexico
[1] => Array
(
[0] => Tamaulipas
)
[2] => USA
)
)
)
使用此功能:
function r($element) {
foreach ($element as $value) {
if (!is_array($value)) {
echo "<li>";
echo $value;
} else {
echo "<ul>";
r($value);
echo "</li>";
echo "</ul>";
}
}
}
PHP代码:
echo "<ul>";
r($array);
echo "</ul>";
返回:
<ul>
<li>Public</li>
<li>User</li>
<li>Content
<ul>
<li>International
<ul>
<li>Mexico
<ul>
<li>Tamaulipas</li>
</ul>
</li>
<li>USA</li>
</ul>
</li>
</ul>
</li>
</ul>
从表面上看,这看起来很简单,但我认为你仍然缺乏信息,无法为你的情况找到最佳答案。首先,您是如何获得阵列的?它来自数据库吗?是否有可能以不同的方式存储,或者您是否被困在这种方式中?深度是否保证现在或将来不会超过1或2,或者您是否计划进行不确定数量的子级别?增加或删除类别的频率如何?我建议您检查一下,因为嵌套模型可能更符合您的需要。非常好的答案,谢谢。你能告诉我,假设数组元素没有深度键,我们如何设置最大深度吗。。我们需要一个$level=0;我想$max_level=3。但是如何在你的功能中放置它们呢?@Baba,他是一个很好的位置。希望你能抽出时间来复习一下。