123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- declare(strict_types=1);
- namespace app\common\lib;
- class Arr
- {
- /**
- * 分类树,支持无限极分类
- * @param $data
- * @return array
- */
- public static function getTree(array $data): array
- {
- $items = [];
- foreach ($data as $v) {
- $items[$v['id']] = $v;
- }
- $tree = [];
- foreach ($items as $id => $item) {
- if (isset($items[$item['pid']])) {
- $items[$item['pid']]['list'][] = &$items[$id];
- } else {
- $tree[] = &$items[$id];
- }
- }
- return $tree;
- }
- /**
- * 对前端的分类数目进行截取
- * @param array $data
- * @param int $firstCount
- * @param int $secondCount
- * @param int $threeCount
- * @return array
- */
- public static function sliceTreeArr(array $data, int $firstCount = 5, int $secondCount = 3, int $threeCount = 5): array
- {
- $data = array_slice($data, 0, $firstCount);
- foreach ($data as $k => $v) {
- if (!empty($v['list'])) {
- $data[$k]['list'] = array_slice($v['list'], 0, $secondCount);
- foreach ($v['list'] as $kk => $vv) {
- if (!empty($vv['list'])) {
- $data[$k]['list'][$kk]['list'] = array_slice($vv['list'], 0, $threeCount);
- }
- }
- }
- }
- return $data;
- }
- }
|