合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
```php <?php /** * Author: Virus * Date: 2020/3/5 * Time: 17:41 */ namespace app\common\lib; class Arr { /** * @desc 无限极分类 * @param $data * @return array */ public static function getTree($data) { $items = []; foreach ($data as $v) { $items[$v['category_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; } /** * @desc 一级分类显示5个,二级分类显示3个,三级分类显示5个 * @param $data * @param int $firstCount * @param int $secondCount * @param int $thirdCount * @return array */ public static function sliceTreeArr($data, $firstCount = 5, $secondCount = 3, $thirdCount = 5) { $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, $thirdCount); } } } } return $data; } /** * 分页默认返回的数据 * @param $num * @return array */ public static function getPaginateDefaultData($num) { $result = [ "total" => 0, "per_page" => $num, "current_page" => 1, "last_page" => 0, "data" => [], ]; return $result; } /** * 数组排序 * @param $result * @param $key * @param int $sort * @return array|bool */ public static function arrsSortByKey($result, $key, $sort = SORT_DESC) { if (!is_array($result) || !$key) { return []; } array_multisort(array_column($result, $key), $sort, $result); return $result; } } ```