2020-09-23 18:02:24 +08:00

86 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库https://gitee.com/zoujingli/ThinkLibrary
// | github 代码仓库https://github.com/zoujingli/ThinkLibrary
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace think\admin\extend;
/**
* 数据处理扩展
* Class DataExtend
* @package think\admin\extend
*/
class DataExtend
{
/**
* 一维数组生成数据树
* @param array $list 待处理数据
* @param string $cid 自己的主键
* @param string $pid 上级的主键
* @param string $sub 子数组名称
* @return array
*/
public static function arr2tree(array $list, string $cid = 'id', string $pid = 'pid', string $sub = 'sub'): array
{
[$tree, $tmp] = [[], array_combine(array_column($list, $cid), array_values($list))];
foreach ($list as $vo) isset($vo[$pid]) && isset($tmp[$vo[$pid]]) ? $tmp[$vo[$pid]][$sub][] = &$tmp[$vo[$cid]] : $tree[] = &$tmp[$vo[$cid]];
unset($tmp, $list);
return $tree;
}
/**
* 一维数组生成数据树
* @param array $list 待处理数据
* @param string $cid 自己的主键
* @param string $pid 上级的主键
* @param string $cpath 当前 PATH
* @param string $ppath 上级 PATH
* @return array
*/
public static function arr2table(array $list, string $cid = 'id', string $pid = 'pid', string $cpath = 'path', string $ppath = ''): array
{
$tree = [];
foreach (static::arr2tree($list, $cid, $pid) as $attr) {
$attr[$cpath] = "{$ppath}-{$attr[$cid]}";
$attr['sub'] = $attr['sub'] ?? [];
$attr['spc'] = count($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, static::arr2table($sub, $cid, $pid, $cpath, $attr[$cpath]));
}
return $tree;
}
/**
* 获取数据树子ID集合
* @param array $list 数据列表
* @param mixed $value 起始有效ID值
* @param string $ckey 当前主键ID名称
* @param string $pkey 上级主键ID名称
* @return array
*/
public static function getArrSubIds(array $list, $value = 0, string $ckey = 'id', string $pkey = 'pid'): array
{
$ids = [intval($value)];
foreach ($list as $vo) if (intval($vo[$pkey]) > 0 && intval($vo[$pkey]) === intval($value)) {
$ids = array_merge($ids, static::getArrSubIds($list, intval($vo[$ckey]), $ckey, $pkey));
}
return $ids;
}
}