无限级分类处理类

9-03 696 views

根据数据库里面的分类递归获取子类

<?php
/*
*静态化方法
*/
class Cate {

    //静态方法获取一维数组递归结果
    static Public function unlimitedForLevel($cate,$html='',$pid=0,$level=0,$parent_name = ''){
        //建立空数组存储结果
        $arr = array();
        //循环$cate,如果这级的pid等于上一级的cid,就先压入数组,再找当前的下一级
        foreach ($cate as $v) {
            if ($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
//                $v['html'] = str_repeat($html, $level);
                if ($v['level'] > 1) {
                    if ($v['level'] == 2) {
                        $v['html'] = '|-   ';
                    } else {
                        $v['html'] = str_repeat('|', $level - 1 ).'   |-';
                    }

                } else {
                    $v['html'] = '';
                }
                $v['parent_name'] = $parent_name;
                $arr[] = $v;
                //递归合并数组
                $arr = array_merge($arr,self::unlimitedForLevel($cate,$html,$v['id'],$level+1,$v['name']));
            }
        }

        return $arr;
    }

    //静态方法获取多维数组递归结果
    static Public function unlimitedForMore($cate,$name='child',$pid=0){
        $arr = array();
        foreach ($cate as $v) {
            if ($v['pid'] == $pid) {
                $v[$name] = self::unlimitedForMore($cate,$name,$v['cid']);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    //递归获取父函数
    static Public function getParents($cate,$cid){
        $arr = array();
        foreach ($cate as $v) {
            if ($v['id'] == $cid) {
                $arr[] = $v;
                $arr = array_merge(self::getParents($cate,$v['parent_id']),$arr);
            }
        }
        return $arr;
    }

}

php-rdkafka测试笔记

参考:https://learnku.com/articles/15176/laravel-implementation-of-kafka-message-push-and-receive-processing 安装 安装librdkafka 库 git clone https...

阅读全文

推荐一个好用的php日志类库,monolog/monolog

github仓库地址 官方文档: https://github.com/Seldaek/monolog/blob/main/doc/01-usage.md 安装 $ composer require monolog/monolog 使用方法 <?php us...

阅读全文

php 二维数组根据某个键值倒叙、升序排序

<?php $array = [ [ 'title' => '标题5', 'views' => 500 ], [ 'title&...

阅读全文

欢迎留言