递归无限极分类

 $v) {
                if ($v['pid'] == $pid) {
                    $v['level'] = $level;
                    $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) {
                    if(!empty(self::unlimitedForLayer($cate, $name, $v['id']))){
                        $v[$name] = self::unlimitedForLayer($cate, $name, $v['id']);
                    }
                    $arr[] = $v;
                }
            }
            return $arr;
        }
        //传递一个子分类ID返回所有的父级分类Id
        Static Public function getParentsId($cate, $id) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['id'] == $id) {
                    if($v['pid'] != 0){
                        $arr[] = $v['pid'];
                        $arr = array_merge(self::getParentsId($cate, $v['pid']), $arr);
                    }
                }
            }
            return $arr;
        }
        //传递一个子分类ID返回所有的父级分类信息  有瑕疵,包含了当前子分类的信息
        //根据位置,删除后 使用
        Static Public function getParents($cate, $id) {
            $arr = array();
            foreach ($cate as $v) {
                if ($v['id'] == $id) {
                    $arr[] = $v;
                    if($v['pid'] != 0){
                        $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;
        }

        public static function test(){
            $arr = [
                ['id'=>1,'name'=>'zjx'],
                ['id'=>2,'name'=>'xml'],
                ['id'=>3,'name'=>'hhh'],
            ];
            array_pop($arr);
            return $arr;
        }
    }

$cate = array(
    0 => array('id' => 1, 'pid' => 0, 'name' => '中国'),
    1 => array('id' => 2, 'pid' => 1, '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' => '张家村'),
    7 => array('id' => 8, 'pid' => 4, 'name' => '天镇县'),
);
//print_r(Category::unlimitedForLevel($cate));
//var_dump(Category::unlimitedForLayer($cate));
$arr = Category::getParents($cate,8);
array_pop($arr);
var_dump($arr);
//var_dump(Category::getParentsId($cate,7));
//var_dump(Category::getChildsId($cate,2));
//var_dump(Category::getChilds($cate,2));

//var_dump(Category::test());

?>

你可能感兴趣的:(递归无限极分类)