• 无限级分类一些循环数组


    <?php
    /*
    * 无限极分类 类
    */
    header("Content-Type: text/html; charset=UTF-8");
    Class Category {

    //组合一维数组
    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['id'] == $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;
    }


    }

    $cate = 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));

  • 相关阅读:
    Class文件和JVM的恩怨情仇
    详解及对比创建线程的三种方式
    浅析Java中线程组(ThreadGroup类)
    简单定义多线程!
    五分钟看懂UML类图与类的关系详解
    LeetCode刷题--14.最长公共前缀(简单)
    LeetCode刷题--13.罗马数字转整数(简答)
    动态规划算法详解及经典例题
    LeetCode--9.回文数(简单)
    LeetCode刷题--7.整数反转(简单)
  • 原文地址:https://www.cnblogs.com/lixihuan/p/7229632.html
Copyright © 2020-2023  润新知