树结构生成类

<?php
namespace util;

/**
* 树结构生成类
*/
class Tree
{
/**
* @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;
}
}


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝