12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace crmeb\utils;
- /**
- * 操作数组帮助类
- * Class Arr
- * @package crmeb\utils
- */
- class Arr
- {
- /**
- * 对数组增加默认值
- * @param array $keys
- * @return array
- */
- public static function getDefaultValue(array $keys, array $configList = [])
- {
- $value = [];
- foreach ($keys as $val) {
- if (is_array($val)) {
- $k = $val[0] ?? '';
- $v = $val[1] ?? '';
- } else {
- $k = $val;
- $v = '';
- }
- $value[$k] = $configList[$k] ?? $v;
- }
- return $value;
- }
- /**
- * 转化iviewUi需要的key值
- * @param $data
- * @return array
- */
- public static function toIviewUi($data)
- {
- $newData = [];
- foreach ($data as $k => $v) {
- $temp = [];
- $temp['path'] = $v['menu_path'];
- $temp['title'] = $v['menu_name'];
- $temp['icon'] = $v['icon'];
- $temp['header'] = $v['header'];
- $temp['is_header'] = $v['is_header'];
- if ($v['is_show_path']) {
- $temp['auth'] = ['hidden'];
- }
- if (!empty($v['children'])) {
- $temp['children'] = self::toIviewUi($v['children']);
- }
- $newData[] = $temp;
- }
- return $newData;
- }
- /**
- * 获取树型菜单
- * @param $data
- * @param int $pid
- * @param int $level
- * @return array
- */
- public static function getTree($data, $pid = 0, $level = 1)
- {
- $childs = self::getChild($data, $pid, $level);
- array_multisort(array_column($childs, 'sort'), SORT_DESC, $childs);
- foreach ($childs as $key => $navItem) {
- $resChild = self::getTree($data, $navItem['id']);
- if (null != $resChild) {
- $childs[$key]['children'] = $resChild;
- }
- }
- return $childs;
- }
- /**
- * 获取子菜单
- * @param $arr
- * @param $id
- * @param $lev
- * @return array
- */
- private static function getChild(&$arr, $id, $lev)
- {
- $child = [];
- foreach ($arr as $k => $value) {
- if ($value['pid'] == $id) {
- $value['level'] = $lev;
- $child[] = $value;
- }
- }
- return $child;
- }
- }
|