博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TP5 Tree类无限极分类
阅读量:6975 次
发布时间:2019-06-27

本文共 6051 字,大约阅读时间需要 20 分钟。

项目中会经常遇到需要递归的地方,分享一个正在用的一个递归查询。虽然递归有时候会慢,但是还没有找到个好的解决办法,如果有好的办法,请分享一下。

$value){
if($value['pid'] == $pid){
self::$treeList[] = $value; unset($data[$key]); self::create($data,$value['id']); } } return self::$treeList; } /** * 分类数据处理 * @end */ /** * @var object 对象实例 */ protected static $instance; /** * 配置参数 * @var array */ protected static $config = [ 'id' => 'id', // id名称 'pid' => 'pid', // pid名称 'title' => 'title', // 标题名称 'child' => 'child', // 子元素键名 'html' => '┝ ', // 层级标记 'step' => 4, // 层级步进数量 ]; /** * 架构函数 * @param array $config */ public function __construct($config = []) {
self::$config = array_merge(self::$config, $config); } /** * 配置参数 * @param array $config * @return object */ public static function config($config = []) {
if (!empty($config)) {
$config = array_merge(self::$config, $config); } if (is_null(self::$instance)) {
self::$instance = new static($config); } return self::$instance; } /** * 将数据集格式化成层次结构 * @param array/object $lists 要格式化的数据集,可以是数组,也可以是对象 * @param int $pid 父级id * @param int $max_level 最多返回多少层,0为不限制 * @param int $curr_level 当前层数 * @return array */ public static function toLayer($lists = [], $pid = 0, $max_level = 0, $curr_level = 0) {
$trees = []; $lists = array_values($lists); foreach ($lists as $key => $value) {
if ($value[self::$config['pid']] == $pid) {
if ($max_level > 0 && $curr_level == $max_level) {
return $trees; } unset($lists[$key]); $child = self::toLayer($lists, $value[self::$config['id']], $max_level, $curr_level + 1); if (!empty($child)) {
$value[self::$config['child']] = $child; } $trees[] = $value; } } return $trees; } /** * 将数据集格式化成列表结构 * @param array|object $lists 要格式化的数据集,可以是数组,也可以是对象 * @param integer $pid 父级id * @param integer $level 级别 * @return array 列表结构(一维数组) */ public static function toList($lists = [], $pid = 0, $level = 0) {
if (is_array($lists)) {
$trees = []; foreach ($lists as $key => $value) {
if ($value[self::$config['pid']] == $pid) {
$title_prefix = str_repeat(" ", $level * self::$config['step']).self::$config['html']; $value['level'] = $level + 1; $value['title_prefix'] = $level == 0 ? '' : $title_prefix; $value['title_display'] = $level == 0 ? $value[self::$config['title']] : $title_prefix.$value[self::$config['title']]; $trees[] = $value; unset($lists[$key]); $trees = array_merge($trees, self::toList($lists, $value[self::$config['id']], $level + 1)); } } return $trees; } else {
foreach ($lists as $key => $value) {
if ($value[self::$config['pid']] == $pid && is_object($value)) {
$title_prefix = str_repeat(" ", $level * self::$config['step']).self::$config['html']; $value['level'] = $level + 1; $value['title_prefix'] = $level == 0 ? '' : $title_prefix; $value['title_display'] = $level == 0 ? $value[self::$config['title']] : $title_prefix.$value[self::$config['title']]; $lists->offsetUnset($key); $lists[] = $value; self::toList($lists, $value[self::$config['id']], $level + 1); } } return $lists; } } /** * 根据子节点返回所有父节点 * @param array $lists 数据集 * @param string $id 子节点id * @return array */ public static function getParents($lists = [], $id = '') {
$trees = []; foreach ($lists as $value) {
if ($value[self::$config['id']] == $id) {
$trees[] = $value; $trees = array_merge(self::getParents($lists, $value[self::$config['pid']]), $trees); } } return $trees; } /** * 获取所有子节点id * @param array $lists 数据集 * @param string $pid 父级id * @return array */ public static function getChildsId($lists = [], $pid = '') {
$result = []; foreach ($lists as $value) {
if ($value[self::$config['pid']] == $pid) {
$result[] = $value[self::$config['id']]; $result = array_merge($result, self::getChildsId($lists, $value[self::$config['id']])); } } return $result; } /** * 获取所有子节点 * @param array $lists 数据集 * @param string $pid 父级id * @return array */ public static function getChilds($lists = [], $pid = '') {
$result = []; foreach ($lists as $value) {
if ($value[self::$config['pid']] == $pid) {
$result[] = $value; $result = array_merge($result, self::getChilds($lists, $value[self::$config['id']])); } } return $result; } /** * 根据子分类获取顶级分类 * @param string $id 子分类id * @return int */ public static function getNavPid($id){
$catId = Category::get($id); if($catId['pid'] != 0){
return self::getNavPid($catId['pid']); } return $catId['id']; } }

转载于:https://www.cnblogs.com/lmpxs/p/6393646.html

你可能感兴趣的文章
如何用 Graylog 管理日志?- 每天5分钟玩转 Docker 容器技术(93)
查看>>
单例模式
查看>>
iOS多线程编程之NSOperation和NSOperationQueue的使用
查看>>
SAP QM 'QM System' 有什么控制作用?
查看>>
Health Check in eShop -- 解析微软微服务架构Demo(五)
查看>>
项目沟通管理计划
查看>>
[20160608]自治事务引起死锁.txt
查看>>
AliGenie AR Fuels the Ali New Retail Strategy - Interactive Marketing Activities
查看>>
一个最简单的通过WireShark破解SSL加密网络数据包的方法
查看>>
教你用TensorFlow和自编码器模型生成手写数字(附代码)
查看>>
荣之联“云桥OneBridge”让IT运维事半功倍
查看>>
中国人工智能学会通讯——人工智能在各医学亚专科的发展现状及趋势 1.3 人工智能在各医学亚专科的发展态势...
查看>>
新技术、新思维开创公共安全管理新模式
查看>>
新产品发布与A轮2000万美元 双喜临门后GrowingIO还要做什么
查看>>
《大数据、小数据、无数据:网络世界的数据学术》一 导读
查看>>
玉山银行的一名新员工“玉山小i随身金融顾问”
查看>>
消除危害 让BYOD策略更安全的几个秘诀
查看>>
云端卫士架构师讲DDoS攻击的智能防御之道
查看>>
《算法技术手册》一2.4.6 二次方的算法性能
查看>>
物联网时代全面降临
查看>>