php無限レベル分類関数(限界なし)
7867 ワード
/**
* [pidToGetTree description]
* @Author Lerko
* @DateTime 2017-04-05T10:44:06+0800
* @param [type] $list [ ]
* @param [type] &$data [ , ]
* @param string $pk [ ]
* @param string $pid [ key]
* @param string $child_key [ key]
* @param string $sort_id [ id]
* @param [type] $sort_type [ php SORT_ASC SORT_DESC]
* @return [type] [description]
*/
function pidToGetTree($list,&$data,$pk='id',$pid='pid',$child_key="_child",$sort_id='sort_id',$sort_type=SORT_ASC,$start_pid=0){
if($data===null){
return;
}
if(count($data)==$start_pid){
//
foreach ($list as $key => &$value) {
if($value[$pid]==0){
$data[]=$value;
unset($list[$key]);
}
}
$sort_root_arr=array_column($data,$sort_id);
array_multisort($sort_root_arr,$sort_type,$data);
}
foreach ($data as $key => &$value) {
foreach ($list as $key2 => $value2) {
if($value2[$pid]==$value[$pk]){
$value[$child_key][]=$value2;
unset($list[$key2]);
}
}
if(empty($value[$child_key])){continue;}
$sort_arr=array_column($value[$child_key],$sort_id);
array_multisort($sort_arr,$sort_type,$value[$child_key]);
pidToGetTree($list,$value[$child_key],$pk,$pid,$child_key,$sort_id,$sort_type);
}
}
入力データ
+----+-----+---------+---------+---------+-------------+---------+
| id | pid | root_id | user_id | sort_id | name_cn | name_en |
+----+-----+---------+---------+---------+-------------+---------+
| 1 | 0 | 2160 | 2160 | 0 | 1 | cat |
| 2 | 1 | 2160 | 2160 | 1 | 1-1 | cat |
| 3 | 1 | 2160 | 2160 | 1 | 1-2 | cat |
| 4 | 1 | 2160 | 2160 | 1 | 1-3 | cat |
| 5 | 0 | 2160 | 2160 | 0 | 5 | cat |
| 6 | 2 | 2160 | 2160 | 2 | 1-1-1 | cat |
| 7 | 5 | 2160 | 2160 | 5 | 5-1 | cat |
| 8 | 5 | 2160 | 2160 | 3 | 5-2 | cat |
| 9 | 0 | 2160 | 2160 | 0 | 9 | cat |
| 10 | 9 | 2160 | 2160 | 9 | 9-1 | cat |
| 11 | 9 | 2160 | 2160 | 9 | 9-2 | cat |
+----+-----+---------+---------+---------+-------------+---------+
出力データ
array (size=3)
0 =>
array (size=8)
'id' => string '1' (length=1)
'pid' => string '0' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '0' (length=1)
'name_cn' => string ' 1' (length=7)
'name_en' => string 'cat' (length=3)
'_child' =>
array (size=3)
0 =>
array (size=7)
'id' => string '2' (length=1)
'pid' => string '1' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '1' (length=1)
'name_cn' => string ' 1-1' (length=9)
'name_en' => string 'cat' (length=3)
1 =>
array (size=7)
'id' => string '3' (length=1)
'pid' => string '1' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '1' (length=1)
'name_cn' => string ' 1-2' (length=9)
'name_en' => string 'cat' (length=3)
2 =>
array (size=7)
'id' => string '4' (length=1)
'pid' => string '1' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '1' (length=1)
'name_cn' => string ' 1-3' (length=9)
'name_en' => string 'cat' (length=3)
1 =>
array (size=8)
'id' => string '5' (length=1)
'pid' => string '0' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '0' (length=1)
'name_cn' => string ' 5' (length=7)
'name_en' => string 'cat' (length=3)
'_child' =>
array (size=2)
0 =>
array (size=7)
'id' => string '7' (length=1)
'pid' => string '5' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '5' (length=1)
'name_cn' => string ' 5-1' (length=9)
'name_en' => string 'cat' (length=3)
1 =>
array (size=7)
'id' => string '8' (length=1)
'pid' => string '5' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '3' (length=1)
'name_cn' => string ' 5-2' (length=9)
'name_en' => string 'cat' (length=3)
2 =>
array (size=8)
'id' => string '9' (length=1)
'pid' => string '0' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '0' (length=1)
'name_cn' => string ' 9' (length=7)
'name_en' => string 'cat' (length=3)
'_child' =>
array (size=2)
0 =>
array (size=7)
'id' => string '10' (length=2)
'pid' => string '9' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '9' (length=1)
'name_cn' => string ' 9-1' (length=9)
'name_en' => string 'cat' (length=3)
1 =>
array (size=7)
'id' => string '11' (length=2)
'pid' => string '9' (length=1)
'root_id' => string '2160' (length=4)
'user_id' => string '2160' (length=4)
'sort_id' => string '9' (length=1)
'name_cn' => string ' 9-2' (length=9)
'name_en' => string 'cat' (length=3)