`
~~~
<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/3-16:13
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace Jrk;
class Trees
{
private static $formatTree;//用于樹型數組完成遞歸格式的全局變量
/**
* @param $arr
* @param int $root
* @return array
* @author: hhygyl
* @name: DeepTree
* @describe:格式化數據
*/
public static function DeepTree($arr, $child="children",$root = 0)
{
$OriginalList = $arr;
$tree = [];//最終數組
$refer = [];//存儲主鍵與數組單元的引用關系
//遍歷
foreach($OriginalList as $k => $v){
if(!isset($v['id']) || !isset($v['pid']) || isset($v[$child])) {
unset($OriginalList[$k]);
continue;
}
$refer[$v['id']] =& $OriginalList[$k];//為每個數組成員建立引用關系
}
//遍歷2
foreach($OriginalList as $k => $v){
if($v['pid']==$root) {//根分類直接添加引用到tree中
$tree[] =& $OriginalList[$k];
}
else {
if(isset($refer[$v['pid']])) {
$parent =& $refer[$v['pid']];//獲取父分類的引用
$parent[$child][] =& $OriginalList[$k];//在父分類的children中再添加一個引用成員
}
}
}
return $tree;
}
/**
* @param $menu
* @param int $id
* @param int $level
* @return array
* @author: hhygyl <hhygyl520@qq.com>
* @name: menulist
* @describe:菜單格式化
*/
public static function menulist($menu,$id=0,$level=0){
static $menus = array();
$size = count($menus)-1;
foreach ($menu as $value) {
if ($value['pid']==$id) {
$value['level'] = $level+1;
if($level == 0)
{
$value['str'] = str_repeat('',$value['level']);
$menus[] = $value;
}
elseif($level == 2)
{
$value['str'] = '    '.'└ ';
$menus[$size]['list'][] = $value;
}
elseif($level == 3)
{
$value['str'] = '      '.'└ ';
$menus[$size]['list'][] = $value;
}
else
{
$value['str'] = '  '.'└ ';
$menus[$size]['list'][] = $value;
}
self::menulist($menu,$value['id'],$value['level']);
}
}
return $menus;
}
/**
* @param $data
* @param int $pid
* @param StringService $field
* @param StringService $pk
* @param StringService $html
* @param int $level
* @param bool $clear
* @return array
* @author: hhygyl
* @name: sortListTier
* @describe: 選擇樹形結構
*/
public static function sortListTier($data, $pid = 0, $field = 'pid', $pk = 'id', $html = '|----', $level = 0, $clear = true)
{
static $list = [];
if ($clear) $list = [];
foreach ($data as $k => $res) {
if ($res[$field] == $pid) {
$res['html'] = str_repeat($html, $level).$res['title'];
$res['level']=$level;
$list[] = $res;
unset($data[$k]);
self::sortListTier($data, $res[$pk], $field, $pk, $html, $level + 1, false);
}
}
return $list;
}
/**
* @param $list
* @param StringService $pk
* @param StringService $pid
* @param StringService $child
* @param int $root
* @return array
* @author: hhygyl
* @name: listToTree
* @describe:把返回的數據集轉換成Tree
*/
public static function listToTree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 創建Tree
$tree = [];
if(is_array($list)) {
// 創建基于主鍵的數組引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] =& $list[$key];
}
foreach ($list as $key => $data) {
// 判斷是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] =& $list[$key];
} else{
if (isset($refer[$parentId])) {
$parent =& $refer[$parentId];
$parent['childs'][] = $data[$pk];
$parent[$child][] =& $list[$key];
}
}
}
}
return $tree;
}
/**
* 將樹子節點加層級成列表
*/
protected static function _toFormatTree($tree, $level = 1) {
foreach ($tree as $key => $value) {
$temp = $value;
if (isset($temp['_child'])) {
$temp['_child'] = true;
$temp['level'] = $level;
} else {
$temp['_child'] = false;
$temp['level'] = $level;
}
array_push(self::$formatTree, $temp);
if (isset($value['_child'])) {
self::_toFormatTree($value['_child'], ($level + 1));
}
}
}
/**
* @param $cat
* @param $next_parentid
* @param StringService $pid
* @param StringService $empty
* @return StringService
* @author: hhygyl
* @name: catEmptyDeal
* @describe:
*/
public static function catEmptyDeal($cat, $next_parentid, $pid='pid', $empty = " ") {
$str = "";
if ($cat[$pid]) {
for ($i=2; $i < $cat['level']; $i++) {
$str .= $empty."│";
}
if ($cat[$pid] != $next_parentid && !$cat['_child']) {
$str .= $empty."└─ ";
} else {
$str .= $empty."├─ ";
}
}
return $str;
}
/**
* @param $list
* @param StringService $title
* @param StringService $pk
* @param StringService $pid
* @param int $root
* @return array|bool
* @author: hhygyl
* @name: toFormatTree
* @describe:轉換成樹
*/
public static function toFormatTree($list,$title = 'title',$pk='id',$pid = 'pid',$root = 0){
if (empty($list)) {
return false;
}
$list =self::listToTree($list,$pk,$pid,'_child',$root);
//dump($list);
self::$formatTree = $data = [];
self::_toFormatTree($list);
foreach (self::$formatTree as $key => $value) {
$index = ($key+1);
$next_parentid = isset(self::$formatTree[$index][$pid]) ? self::$formatTree[$index][$pid] : '';
$value['level_show'] =self::catEmptyDeal($value, $next_parentid);
$value['title_show'] = $value['level_show'].$value[$title];
$data[] = $value;
}
return $data;
}
/**
* @param $data
* @param int $pid
* @param StringService $field
* @param StringService $pk
* @param int $level
* @return array
* @author: hhygyl
* @name: getChindNode
* @describe:分級返回多維數組
*/
public static function getChindNode($data, $pid = 0, $field = 'pid', $pk = 'id', $level = 1)
{
static $list = [];
foreach ($data as $k => $res) {
if ($res['pid'] == $pid) {
$list[] = $res;
unset($data[$k]);
self::getChindNode($data, $res['id'], $field, $pk, $level + 1);
}
}
return $list;
}
/**
* @param $data
* @param $pid
* @param string $field
* @param string $pk
* @return array
* @author: hhygyl <hhygyl520@qq.com>
* @name: getChildrenPid
* @describe:分級返回下級所有分類ID
*/
public static function getChildrenPid($data,$pid, $field = 'pid', $pk = 'id')
{
static $pids = [];
foreach ($data as $k => $res) {
if ($res[$field] == $pid) {
$pids[]=$res[$pk];
self::getChildrenPid($data, $res[$pk], $field, $pk);
}
}
return $pids;
}
/**
* @param $list
* @param string $id
* @param string $pid
* @param string $son
* @return array|mixed
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/17
* @name: arr2tree
* @describe:一維數據數組生成數據樹
*/
public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'sub')
{
list($tree, $map) = [[], []];
foreach ($list as $item) $map[$item[$id]] = $item;
foreach ($list as $item) if (isset($item[$pid]) && isset($map[$item[$pid]])) {
$map[$item[$pid]][$son][] = &$map[$item[$id]];
} else $tree[] = &$map[$item[$id]];
unset($map);
return $tree;
}
/**
* 一維數據數組生成數據樹
* @param array $list 數據列表
* @param string $id ID Key
* @param string $pid 父ID Key
* @param string $path
* @param string $ppath
* @return array
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/17
* @name: arr2tree
* @describe:一維數據數組生成數據樹
*/
public static function arr2table(array $list, $id = 'id', $pid = 'pid', $path = 'path', $ppath = '')
{
$tree = [];
foreach (self::arr2tree($list, $id, $pid) as $attr) {
$attr[$path] = "{$ppath}-{$attr[$id]}";
$attr['sub'] = isset($attr['sub']) ? $attr['sub'] : [];
$attr['spt'] = substr_count($ppath, '-');
$attr['spl'] = str_repeat(" ├ ", $attr['spt']);
$sub = $attr['sub'];
unset($attr['sub']);
$tree[] = $attr;
if (!empty($sub)) $tree = array_merge($tree, self::arr2table($sub, $id, $pid, $path, $attr[$path]));
}
return $tree;
}
}
~~~
`
- 空白目錄
- thinkphp5
- tools-常用類庫
- redis類庫
- Excel類庫
- File文件操作類庫
- Http請求類庫
- Maile郵件發送
- Hooks行為鉤子
- 七牛云
- 隨機數和字符串生成
- 字符串處理
- 時間類處理
- tree型轉換
- 工具類庫
- 文件打包下載
- 常用功能
- 文件上傳
- php生成word文檔
- elasticsearch 基本搜索
- 使用jwt開發API接口
- 安裝模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 漢字轉拼音
- 安裝使用
- Pinyin類
- elasticsearch操作
- 常用方法
- 數據源生成layui-select
- 獲取自定義配置項
- 百度編輯器
- 格式化文件大小
- 多語言設置
- hook監聽
- 域名綁定到模塊
- thinkphp6
- 文件上傳
- tp5totp6
- 創建路徑
- 獲取類所有方法
- password_hash加密驗證
- 生成 qrcode
- 郵件發送
- 獲取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加鎖
- 百度翻譯
- QueryList爬取數據
- 獲取時間類
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 驗證 cpf,cnpj
- php常用方法
- 日志
- 卡通頭像
- 兩位小數
- 圖片轉base64
- auth加密解密
- phpoffice/phpspreadsheet導入導出
- fastadmin
- 樹結構
- 單選框
- 復選框
- 二級搜索
- select選擇框
- selectpage選中回調事件
- 標簽添加
- 修改where條件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板