php 递归排序
/**
* 排序层级排序
* @param $arr 数组
* @param $id id
* @param $level 层级
* @return array
*/
function sortPid($arr, $id, $level = 0)
{
$data =array();
foreach ($arr as $k=>$v) {
if ($v['parent_id'] == $id) {
$v['level']=$level;
$v['children'] = sortPid($arr, $v['id'], $level+1);
$data[] = $v;
}
}
return $data;
}
/**
* 同级排序
* @param $arr 数组
* @param $id id
* @param $level 层级
* @return array
*/
function getTree($arr, $id, $level = 0)
{
static $data;
foreach ($arr as $key=>$val) {
if ($val['parent_id'] == $id) {
$flg = str_repeat('┗━', $level);
$val['title'] = $flg.$val['title'];
$data[] = $val;
getTree($arr, $val['id'], $level+1);
}
}
return $data;
}
/**
* 获取子类
* @param $arr 数组
* @param $id id
* @param $level 层级
* @return array
*/
function getSon($array, $id)
{
static $data;
foreach ($array as $k => $v) {
if ($v['parent_id'] == $id) {
$data[] = $array[$k];
getSon($array, $v['id']);
}
}
return $data;
}