关于php评论回复无限极嵌套如何实现?已写基本代码,期待高手解惑

PHP中文网
Release: 2023-02-28 16:08:02
Original
1768 people have browsed it

问题:

下面是有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'
    )
);
Copy after login

最笨的办法,实现我要的效果

$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 &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($sub);
Copy after login

打印出来的就是我要的效果,子评论作为父评论的子数组,但像我这么写没法嵌套多层评论,无限极嵌套要这么实现呢?
请求帮助,感激不尽!

解决方案:

是不是可以理解为 无限级 分类的问题?

// 无限级分类
    $list = array(
        array(&#39;id&#39;=>1, &#39;fid&#39;=>0, &#39;title&#39; => &#39;中国&#39;), 
        array(&#39;id&#39;=>2, &#39;fid&#39;=>1, &#39;title&#39; => &#39;江苏&#39;),
        array(&#39;id&#39;=>3, &#39;fid&#39;=>1, &#39;title&#39; => &#39;安徽&#39;),
        array(&#39;id&#39;=>4, &#39;fid&#39;=>8, &#39;title&#39; => &#39;江阴&#39;),
        array(&#39;id&#39;=>5, &#39;fid&#39;=>3, &#39;title&#39; => &#39;芜湖&#39;),
        array(&#39;id&#39;=>6, &#39;fid&#39;=>3, &#39;title&#39; => &#39;合肥&#39;),
        array(&#39;id&#39;=>7, &#39;fid&#39;=>3, &#39;title&#39; => &#39;蚌埠&#39;),
    );
 
    $new = array();
    foreach($list as $v){
        $new[$v[&#39;fid&#39;]][] = $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 &#39;</ul>&#39;;
        }else{
            for($k=$p[$i]; $k<count($var);$k++){
                if($k == 0)
                    echo &#39;<ul>&#39;;
                echo &#39;<li>&#39;.$var[$k][&#39;title&#39;];
                $p[$i]++;
                if(isset($new[$var[$k][&#39;id&#39;]])){
                    $i = $var[$k][&#39;id&#39;];
                    $j++;
                    $q[$j] = $i;
                    $next = true;
                    break;
                }
                echo &#39;</li>&#39;;
                if($k == count($var)-1){
                    echo &#39;</ul>&#39;;
                }
            }
        }
        if($next){
            continue;
        }
        $j--;
        if($j < 0){
            break;
        }
    }
     
    exit;
Copy after login

相关文章:

PHP评论回复解决方法

使用php无线级别分门别类 实现评论回复功能

使用JS完美实现仿QQ空间评论回复特效

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!