CI 实现无限级分类 \ 数据处理组合

CI 实现无限级分类 \ 数据处理组合

<?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    /**
     * Category Controller
     * 
     */
    class Category extends MY_Controller {

        public function __construct(){
            // $this->isNeedLogin = TRUE;
            parent::__construct();
        }

        public function index()
        {
            $cate1 = array(
                0 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
                1 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
                2 => array('id' => 3, 'pid' => 1, 'name' => '上饶市'),
                3 => array('id' => 4, 'pid' => 3, 'name' => '广丰县'),
                4 => array('id' => 5, 'pid' => 2, 'name' => '杭州市'),
                5 => array('id' => 6, 'pid' => 5, 'name' => '西湖'),
                6 => array('id' => 7, 'pid' => 6, 'name' => '断桥'),
             );
            
            print_r(Category::unlimitedForLevel($cate));
            print_r(Category::unlimitedForLayer($cate));
            //print_r(Category::getParents($cate,7));
            //print_r(Category::getChildsId($cate,2));
            //print_r(Category::getChilds($cate,2));
        }

        //组合一维数组
        Static Public function unlimitedForLevel ($cate, $html = '--', $pid = 0, $level = 0) {
            $arr = array();
            foreach ($cate as $k => $v) {
                if ($v['pid'] == $pid) {
                    $v['level'] = $level + 1;
                    $v['html']  = str_repeat($html, $level);
                    $arr[] = $v;
                    $arr = array_merge($arr, self::unlimitedForLevel($cate, $html, $v['id'], $level + 1));
                }
            }
            return $arr;
        }
        //组合多维数组
        Static Public function unlimitedForLayer ($cate, $name = 'child', $pid = 0) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['pid'] == $pid) {
                    $v[$name] = self::unlimitedForLayer($cate, $name, $v['id']);
                    $arr[] = $v;
                }
            }
            return $arr;
        }
        //传递一个子分类ID返回所有的父级分类  
        Static Public function getParents ($cate, $id) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['pid'] == $id) {
                    $arr[] = $v;
                    $arr = array_merge(self::getParents($cate, $v['pid']), $arr); 
                }
            }
            return $arr;
        }
        //传递一个父级分类ID返回所有子分类ID
        Static Public function getChildsId ($cate, $pid) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['pid'] == $pid) {
                    $arr[] = $v['id'];
                    $arr = array_merge($arr, self::getChildsId($cate, $v['id']));
                }
            }
            return $arr;
        }
        //传递一个父级分类ID返回所有子分类
        Static Public function getChilds ($cate, $pid) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['pid'] == $pid) {
                    $arr[] = $v;
                    $arr = array_merge($arr, self::getChilds($cate, $v['id']));
                }
            }
            return $arr;
        }
    }
    



/* End of file User.php */
/* Location: ./application/controllers/User.php */

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值