第一种:
public function comment() {
$data = $this->com_list();
$this->succ('操作成功',$data);
}
protected function com_list($pid = 0, &$result = []) {
$comment = Db::name('comment')->order('create_time desc')->where(['pid'=>$pid])->select();
if (empty($comment)) {
return array();
}
foreach ($comment as $k=>$v) {
$arr = &$result[];
$v['children'] = $this->com_list($v['id'],$arr);
$arr = $v;
}
return $result;
}
第二种:
public function comment() {
$wid = param_check('wid'); //作品id
$comment = Db::name('comment')->order('create_time desc')->where(['wid'=>$wid])->select();
$data = $this->com_list($comment,0);
$this->succ('操作成功',$data);
}
protected function com_list($data, $pid = 0) {
$node = [];
foreach ($data as $k=>$v) {
if ($pid == $v['pid']) {
$node[$k] = $v;
$node[$k]['children'] = $this->com_list($data,$v ['id']);
}
}
return $node;
}