| // +------------------------------------------------+ namespace tools; /** * 生成多层树状下拉选框的工具 */ class Tree { /** * 用于树型数组完成递归格式的全局变量 */ private static $formatTree; /** * 生成多层树,供下拉选框使用 */ public static function toFormatTree($list, $title = 'title', $pk = 'id', $pid = 'pid', $root = 0) { $list = self::list2tree($list, $pk, $pid, '_child', $root); self::$formatTree = []; self::_toFormatTree($list, 0, $title); return self::$formatTree; } /** * 把数据集转换成Tree * @param array $list 要转换的数据集 * @param string $pk [description] * @param string $pid [description] * @param string $child [description] * @param integer $root [description] * @return array */ public static function list2tree($list, $pk = 'id', $pid = 'pid', $child = 'children', $root = 0) { $tree = []; if (is_array($list)) { $refer = []; foreach ($list as $key => $data) { $refer[$data[$pk]] = &$list[$key]; } foreach ($list as $key => $data) { $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 $list * @param integer $level 进行递归时传递用的参数 * @author 小陈叔叔 */ private static function _toFormatTree($list, $level = 0, $title = 'title') { foreach ($list as $key => $val) { $tmp_str = str_repeat(" ", $level * 4); $tmp_str .= "└ "; $val['level'] = $level; $val['title_show'] = $level == 0 ? $val[$title] . " " : $tmp_str . $val[$title]; if (!array_key_exists('_child', $val)) { array_push(self::$formatTree, $val); } else { $tmp_ary = $val['_child']; unset($val['_child']); array_push(self::$formatTree, $val); self::_toFormatTree($tmp_ary, $level + 1, $title); //进行下一层递归 } } return; } }