二维数组转为树形if (!function_exists('tree')) {
function tree(array $array, $superior_id = 0, $superior_key = 'superior_id', $primary_key = 'cate_id', $son_key = 'son') :array
{
$return = [];
foreach ($array as $k => $v) {
if ($v[$superior_key] == $superior_id) {
$son = tree($array, $v[$primary_key], $superior_key, $primary_key, $son_key);
if ($son) $v[$son_key] = $son;
$return[] = $v;
}
}
return $return;
}
}
树形结构转为二维数组if (!function_exists('un_tree')) {
function un_tree(array $array, $son_key = 'son', $times = 0) :array
{
$return = [];
$times += 1;
if (!empty($array)) {
foreach ($array as $key => $value) {
$son = isset($value[$son_key]) ? $value[$son_key] : '';
if ($son) unset($value[$son_key]);
$value['times'] = $times;
array_push($return, $value);
if ($son) $return = array_merge($return, un_tree($son, $son_key, $times));
}
}
return $return;
}
}