Commit 247b3b97 authored by 王源's avatar 王源 🎧

添加list_go_tree

parent aa5e9f8e
...@@ -310,25 +310,33 @@ if (!function_exists('get_tree_id')) { ...@@ -310,25 +310,33 @@ if (!function_exists('get_tree_id')) {
} }
} }
if (!function_exists('get_tree')) { if (!function_exists('list_go_tree')) {
/** /**
* 树状的算法 * 列表转树状格式
* @param array $array 代转化数组 * @param array $list
* @param int $pid 起始节点 * @param string $pk
* @param string $pid
* @param string $children
* @return array * @return array
*/ */
function get_tree($array = [], $pid = 0) function list_go_tree(array $list = [], $pk = 'id', $pid = 'parent_id', $children = 'children')
{ {
$list = []; $tree = $refer = [];
// 获取每个节点的直属子节点,*记住是直属,不是所有子节点 // 创建基于主键的数组引用
foreach ($array as $item) { foreach ($list as $key => $data) {
if (isset($list[$item['pid']])) { $refer[$data[$pk]] = &$list[$key];
$list[$item['pid']][] = $item; }
foreach ($list as $key => $data) {
$parentId = $data[$pid];
// 判断是否存在parent
if (isset($refer[$parentId])) {
$parent = &$refer[$parentId];
$parent[$children][] = &$list[$key];
} else { } else {
$list[$item['pid']] = [$item]; $tree[] = &$list[$key];
} }
} }
return format_tree($list); return $tree;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment