无限级分类处理类

9-03 215 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使用生成器读取超大文件

<?php function getLines($file) { $f = fopen($file, 'r'); try { while ($line = fgets($f)) { yield $line; ...

阅读全文

php获取字符串里面的图片,xpath方式

代码 <?php $html = <<<str <html> <head> <title>123</title> </head> <body> <img src=&quo...

阅读全文

Thinkphp5 in查询结果按照in里面的顺序排

或许有人会注意过,但我以前真不知道 SQL: select * from table where id IN (3,6,9,1,2,5,8,7); 这样的情况取出来后,其实,id还是按1,2,3,4,5,6,7,8,9,排序...

阅读全文

欢迎留言