1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-24 12:05:39 +08:00
course-tencent-cloud/app/Builders/CategoryTreeList.php
2020-03-16 15:33:36 +08:00

45 lines
1.1 KiB
PHP

<?php
namespace App\Builders;
class CategoryTreeList extends Builder
{
public function handleTreeList($categories)
{
$list = [];
foreach ($categories as $category) {
if ($category['parent_id'] == 0) {
$key = $category['id'];
$list[$key] = [
'id' => $category['id'],
'name' => $category['name'],
'priority' => $category['priority'],
'children' => [],
];
} else {
$key = $category['parent_id'];
$list[$key]['children'][] = [
'id' => $category['id'],
'name' => $category['name'],
'priority' => $category['priority'],
];
}
}
usort($list, function ($a, $b) {
return $a['priority'] > $b['priority'];
});
foreach ($list as $key => $value) {
usort($list[$key]['children'], function ($a, $b) {
return $a['priority'] > $b['priority'];
});
}
return $list;
}
}