企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ /** * 将数组转为tree树形结构函数 * * @param $list * @param string $pk * @param string $pid * @param string $child * @param int $root * * @return array */ function listToTree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0) { $tree = array(); 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[$child][] =& $list[$key]; } } } } return $tree; } ~~~ ~~~ /** * 数组层级缩进转换 * @param array $array 源数组 * @param int $pid * @return array */ function array2level($array, $pid = 0, $level = 1) { static $list = []; foreach ($array as $v) { if ($v['pid'] == $pid) { $v['level'] = $level; $list[] = $v; self::array2level($array, $v['id'], $level + 1); } } return $list; } /** * 构建层级(树状)数组 * @param array $array 要进行处理的一维数组,经过该函数处理后,该数组自动转为树状数组 * @param string $pid_name 父级ID的字段名 * @param string $child_key_name 子元素键名 * @return array|bool */ function array2tree(&$array, $pid_name = 'pid', $child_key_name = 'children') { $counter = self::array_children_count($array, $pid_name); if (!isset($counter[0]) || $counter[0] == 0) { return $array; } $tree = []; while (isset($counter[0]) && $counter[0] > 0) { $temp = array_shift($array); if (isset($counter[$temp['id']]) && $counter[$temp['id']] > 0) { array_push($array, $temp); } else { if ($temp[$pid_name] == 0) { $tree[] = $temp; } else { $array = self::array_child_append($array, $temp[$pid_name], $temp, $child_key_name); } } $counter = self::array_children_count($array, $pid_name); } return $tree; } /** * 把元素插入到对应的父元素$child_key_name字段 * @param $parent * @param $pid * @param $child * @param string $child_key_name 子元素键名 * @return mixed */ function array_child_append($parent, $pid, $child, $child_key_name) { foreach ($parent as &$item) { if ($item['id'] == $pid) { if (!isset($item[$child_key_name])) { $item[$child_key_name] = []; } $item[$child_key_name][] = $child; } } return $parent; } /** * 子元素计数器 * @param array $array * @param int $pid * @return array */ function array_children_count($array, $pid) { $counter = []; foreach ($array as $item) { $count = isset($counter[$item[$pid]]) ? $counter[$item[$pid]] : 0; $count++; $counter[$item[$pid]] = $count; } return $counter; } ~~~