Php 如何为注释构建树结构?
我正在获取博客帖子的所有评论,并通过使用以下函数的引用将它们放在树状结构中:Php 如何为注释构建树结构?,php,Php,我正在获取博客帖子的所有评论,并通过使用以下函数的引用将它们放在树状结构中: public function getComments($articleID) { $sql = "SELECT * FROM comments WHERE articleid = $articleID"; $database = DatabaseFactory::getFactory()->Connect(); $stmt = $database->query($sql);
public function getComments($articleID) {
$sql = "SELECT * FROM comments WHERE articleid = $articleID";
$database = DatabaseFactory::getFactory()->Connect();
$stmt = $database->query($sql);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($res!=NULL)
{
$references = array();
$tree = array();
foreach ($res as $id=> &$node) {
// Use id as key to make a references to the tree and initialize it with node reference.
$references[$node['comment_id']] = &$node;
// Add empty array to hold the children/subcategories
$node['children'] = array();
// Get your root node and add this directly to the tree
if ($node['comment_respond_to']==0) {
$tree[$node['comment_id']] = &$node;
} else {
// Add the non-root node to its parent's references
$references[$node['comment_respond_to']]['children'][$node['comment_id']] = &$node;
}
}
return $tree;
当Iprint\r($tree)时代码>这是输出
Array
(
[1] => Array
(
[comment_id] => 1
[articleid] => 1
[user_id] => 2
[comment_comment] => First comment.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 0
[children] => Array
(
[2] => Array
(
[comment_id] => 2
[articleid] => 1
[user_id] => 1
[comment_comment] => First comment, first child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 1
[children] => Array
(
)
)
[4] => Array
(
[comment_id] => 4
[articleid] => 1
[user_id] => 1
[comment_comment] => First comment, second child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 1
[children] => Array
(
)
)
)
)
[3] => Array
(
[comment_id] => 3
[articleid] => 1
[user_id] => 2
[comment_comment] => Second comment.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 0
[children] => Array
(
[6] => Array
(
[comment_id] => 6
[articleid] => 1
[user_id] => 1
[comment_comment] => Second comment, first child.
[comment_date] => 2016-03-10 20:06:43
[comment_respond_to] => 3
[children] => Array
(
)
)
)
)
)
一切都很完美
是时候回音了
我的期望输出:
第一条评论
第一个评论,第一个孩子
第一个评论,第二个孩子
第二条评论
第二个评论,第一个孩子
所以,我知道我的下一步应该是使用foreach创建循环并获取注释
我替换返回$tree代码>带有
foreach ($tree as $key) {
echo '<li>'.$key["comment_comment"], '</li>';
}
foreach($tree as$key){
回显“”.$key[“comment_comment”],“ ”;
}
它输出
第一条评论
第二条评论
缺少子注释。
我知道我的下一步应该是在当前foreach中创建另一个foreach来获取子评论,但我不知道如何做
非常感谢您的帮助。您应该使用foreach中的key=>value
语法。
这样做:
foreach ($tree as $key) {
echo '<li>'.$key["comment_comment"], '</li>';
if($key['children']) {
echo '<ul>';
foreach($key['children'] as $child) {
echo '<li>'.$child["comment_comment"], '</li>';
}
echo '</ul>';
}
}
foreach($tree as$key){
回显“”.$key[“comment_comment”],“ ”;
如果($key['children'])){
回声“”;
foreach($key['children']作为$child){
回显“- ”.$child[“comment_comment”],“
”;
}
回声“
”;
}
}
//编辑:
如果有超过1个子级别,您应该使用递归循环。一个简单的解决方案是使用,例如
我绞尽脑汁想弄明白为什么,但这段代码调用了每一列并列出了它们。可以打印整数,但不能对其本身进行注释。输出:第156行也是echo“”.$child[“comment_comment”],“ ”代码>
<?php
$comments = data();
foo($comments);
function foo($arr, $level=0) {
$prepend = str_repeat(' ', $level); // <- the $level thingy is not necessary; it's only in here to get a bit prettier output
echo $prepend, '<ul>', PHP_EOL;
foreach($arr as $comment) {
echo $prepend, ' <li>', $comment['comment_date'], ' ', htmlentities($comment['comment_comment']), PHP_EOL;
if ( !empty($comment['children']) ) {
foo($comment['children'], $level+1); // recurse into the next level
}
echo $prepend, ' </li>', PHP_EOL;
}
echo $prepend, '</ul>', PHP_EOL;
}
function data() {
return array (
1 =>
array (
'comment_id' => 1,
'articleid' => 1,
'user_id' => 2,
'comment_comment' => 'First comment.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 0,
'children' =>
array (
2 =>
array (
'comment_id' => 2,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'First comment, first child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 1,
'children' =>
array (
),
),
4 =>
array (
'comment_id' => 4,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'First comment, second child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 1,
'children' =>
array (
14 =>
array (
'comment_id' => 14,
'articleid' => 1,
'user_id' => 99,
'comment_comment' => 'Comment to second child of First comment.',
'comment_date' => '2016-03-10 20:19:43',
'comment_respond_to' => 4,
'children' =>
array (
),
),
),
),
),
),
3 =>
array (
'comment_id' => 3,
'articleid' => 1,
'user_id' => 2,
'comment_comment' => 'Second comment.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 0,
'children' =>
array (
6 =>
array (
'comment_id' => 6,
'articleid' => 1,
'user_id' => 1,
'comment_comment' => 'Second comment, first child.',
'comment_date' => '2016-03-10 20:06:43',
'comment_respond_to' => 3,
'children' =>
array (
),
),
),
),
);
}
<ul>
<li>2016-03-10 20:06:43 First comment.
<ul>
<li>2016-03-10 20:06:43 First comment, first child.
</li>
<li>2016-03-10 20:06:43 First comment, second child.
<ul>
<li>2016-03-10 20:19:43 Comment to second child of First comment.
</li>
</ul>
</li>
</ul>
</li>
<li>2016-03-10 20:06:43 Second comment.
<ul>
<li>2016-03-10 20:06:43 Second comment, first child.
</li>
</ul>
</li>
</ul>