/**
* [makeTree 递归+父级]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T16:44:40+0800
* @param array $data [二维数组]
* @param array $options [配置信息]
* @return [type] [description]
*/
class DataTree {
// 初始化配置信息
protected static $config = array(
/* 父键默认值 */
'parent_val' => '0',
/* 主键 */
'primary_key' => 'id',
/* 父键 */
'parent_key' => 'pid',
/* 展开属性 */
'expanded_key' => 'expanded',
/* 叶子节点属性 */
'leaf_key' => 'leaf',
/* 孩子节点属性 */
'children_key' => 'child',
/* 是否展开子节点 */
'expanded' => false,
/* 字段名称 */
'name' => 'department',
/* 分隔符 */
'tag' => '->'
);
/* 结果集 */
protected static $result = array();
/* 层次暂存 */
protected static $level = array();
/**
* [makeTree 生成树形结构]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T16:44:40+0800
* @param array $data [二维数组]
* @param array $options [配置信息]
* @return [type] [description]
*/
public function dataTree($data = array(),$options=array() )
{
$dataset = self::buildData($data,$options);
$data = self::makeTreeCore(self::$config['parent_val'],$dataset,'normal');
return $data;
}
/**
* [makeTreeForHtml 生成线性结构, 便于HTML输出, 参数同上]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T16:45:31+0800
* @param [type] $data [description]
* @param array $options [description]
* @return [type] [description]
*/
public function dataTreeForHtml($data = array(),$options=array())
{
$dataset = self::buildData($data,$options);
$data = self::makeTreeCore(self::$config['parent_val'],$dataset,'linear');
return $data;
}
/**
* [buildData 格式化数据, 私有方法]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T16:46:49+0800
* @param [type] $data [description]
* @param [type] $options [description]
* @return [type] [description]
*/
private static function buildData($data,$options)
{
$config = array_merge(self::$config,$options);
self::$config = $config;
extract($config);
// print_r($data);
$r = array();
foreach($data as $item){
$id = $item[$primary_key];
$parent_id = $item[$parent_key];
$r[$parent_id][$id] = $item;
}
return $r;
}
/**
* [makeTreeCore 生成树核心, 私有方法]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T16:47:02+0800
* @param [type] $index [description]
* @param [type] $data [description]
* @param string $type [description]
* @return [type] [description]
*/
private static function makeTreeCore($index = 0,$data = array(),$type='linear')
{
extract(self::$config);
if(empty($data[$index])){
return [];
}
foreach($data[$index] as $id=>$item)
{
// 防止上级用户是自己本人而进入死循环
if($item[$primary_key] == $item[$parent_key]){
continue ;
}
if($type=='normal'){
if(isset($data[$id]))
{
// $item[$expanded_key]= self::$config['expanded'];//展开属性
$item[$leaf_key]= 0;
$item[$children_key]= self::makeTreeCore($id,$data,$type);
}
else
{
$item[$leaf_key]= 1;
$item['child']= array();
}
$r[] = $item;
}else if($type=='linear'){
$parent_id = $item[$parent_key];
self::$level[$id] = $index == self::$config['parent_val'] ? 0 : self::$level[$parent_id]+1;
$item['level'] = self::$level[$id];
// print_r($item);
self::$result['datainfo'][] = $item;
if(isset($data[$id])){
self::makeTreeCore($id,$data,$type);
}
$r = self::$result;
}
}
return $r;
}
/**
* [data_parent 获取父级]
* @Author shao
* @email 1707790075@qq.com
* @DateTime 2018-08-08T17:11:51+0800
* @param array $data [description]
* @return [type] [description]
*/
public function dataParent($data = [],$options = array())
{
$config = array_merge(self::$config,$options);
self::$config = $config;
extract($config);
$list = $data;
$pid = isset($config['parent_val']) ? $config['parent_val'] : 0;
$id_name = isset($config['primary_key']) ? $config['primary_key'] : 'id';
$pid_name = isset($config['parent_key']) ? $config['parent_key'] : 'pid';
$name = isset($config['name']) ? $config['name'] : 'department';
$tag = isset($config['tag']) ? $config['tag'] : '->';
if (empty($list)) return $list;
//初始化数据
$arr_id_to_pid = array();
$arr_id_to_name = array();
foreach ($list as $k => $v) {
$arr_id_to_pid[$v[$id_name]] = $v[$pid_name];
$arr_id_to_name[$v[$id_name]] = $v[$name];
}
//获取父级
foreach ($list as $k => $v) {
$tmp_name = '';
$id = $v[$pid_name];
$i = 0;
while ($id > 0) {
if ($id == $pid) break;
$tmp_name = $tmp_name && isset($arr_id_to_name[$id]) ? $arr_id_to_name[$id].$tag.$tmp_name : $arr_id_to_name[$id];
$id = isset($arr_id_to_pid[$id]) ? $arr_id_to_pid[$id] : 0;
$i++;
if ($i > 10000) break;
}
$list[$k]['parent_name'] = $tmp_name;
}
return $list;
}
}
调用
// 实例化递归查询
$DataTree = new DataTree();
// 配置参数
$config = array();
$config['parent_val'] = $uid;
$config['primary_key'] = 'id';
$config['parent_key'] = 'parentid';
$config['name'] = 'department';
return $DataTree->dataTreeForHtml($array,$config);