关于php评论回复无限极嵌套如何实现?已写基本代码,期待高手解惑
问题:
下面是有3层嵌套的评论,parent=0表示一级评论,parent=3表示父评论的id为3
$comments = array ( array ( 'id' => '3', 'parent' => '0' ), array ( 'id' => '9', 'parent' => '0' ), array ( 'id' => '1', 'parent' => '3' ), array ( 'id' => '2', 'parent' => '3' ), array ( 'id' => '5', 'parent' => '1' ) , array ( 'id' => '7', 'parent' => '1' ) );
最笨的办法,实现我要的效果
$sub = array (); foreach ($comments as $a) { if ($a['parent'] == 0) { foreach ($comments as $b) { if ($b['parent'] == $a['id']) { foreach ($comments as $c) { if ($c['parent'] == $b['id']) { $b['children'][] = $c; } } $a['children'][] = $b; } } $sub[] = $a; } } echo '<pre class="brush:php;toolbar:false">'; print_r($sub);
打印出来的就是我要的效果,子评论作为父评论的子数组,但像我这么写没法嵌套多层评论,无限极嵌套要这么实现呢?
请求帮助,感激不尽!
解决方案:
是不是可以理解为 无限级 分类的问题?
// 无限级分类 $list = array( array('id'=>1, 'fid'=>0, 'title' => '中国'), array('id'=>2, 'fid'=>1, 'title' => '江苏'), array('id'=>3, 'fid'=>1, 'title' => '安徽'), array('id'=>4, 'fid'=>8, 'title' => '江阴'), array('id'=>5, 'fid'=>3, 'title' => '芜湖'), array('id'=>6, 'fid'=>3, 'title' => '合肥'), array('id'=>7, 'fid'=>3, 'title' => '蚌埠'), ); $new = array(); foreach($list as $v){ $new[$v['fid']][] = $v; } $i = 0; $j = 0; $a = true; $p[$i] = 0; $q[$j] = $i; while($a){ $next = false; $i = $q[$j]; $var = $new[$i]; if(!isset($p[$i])){ $p[$i] = 0; } if($p[$i] == count($var)){ echo '</ul>'; }else{ for($k=$p[$i]; $k<count($var);$k++){ if($k == 0) echo '<ul>'; echo '<li>'.$var[$k]['title']; $p[$i]++; if(isset($new[$var[$k]['id']])){ $i = $var[$k]['id']; $j++; $q[$j] = $i; $next = true; break; } echo '</li>'; if($k == count($var)-1){ echo '</ul>'; } } } if($next){ continue; } $j--; if($j < 0){ break; } } exit;
相关文章:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
