无限级分类处理类

9-03 1,278 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获取包含10w个手机号的txt文件(逗号隔开)

txt文件格式如下 13000000000,13000000005,13000000403,13001010129,13001010688,13001010838,13001016389,13001019538,13001030005,13001042700,13001047495...

阅读全文

php使用Carbon获取2个日期之间的日期

<?php use Carbon\CarbonPeriod; $period = CarbonPeriod::create('2018-06-14', '2018-06-20'); // Iterate over the period fore...

阅读全文

php 解决超卖的几种方案(redis锁、mysql悲观锁)

1.创建商品和订单表模拟商品交易操作 fa_goods商品表,stock商品库存,test_order=订单表 2.添加一个测试商品数据(商品数量为150个) 测试并发购买的方法(gol...

阅读全文

欢迎留言