Arr.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace crmeb\utils;
  3. /**
  4. * 操作数组帮助类
  5. * Class Arr
  6. * @package crmeb\utils
  7. */
  8. class Arr
  9. {
  10. /**
  11. * 对数组增加默认值
  12. * @param array $keys
  13. * @return array
  14. */
  15. public static function getDefaultValue(array $keys, array $configList = [])
  16. {
  17. $value = [];
  18. foreach ($keys as $val) {
  19. if (is_array($val)) {
  20. $k = $val[0] ?? '';
  21. $v = $val[1] ?? '';
  22. } else {
  23. $k = $val;
  24. $v = '';
  25. }
  26. $value[$k] = $configList[$k] ?? $v;
  27. }
  28. return $value;
  29. }
  30. /**
  31. * 转化iviewUi需要的key值
  32. * @param $data
  33. * @return array
  34. */
  35. public static function toIviewUi($data)
  36. {
  37. $newData = [];
  38. foreach ($data as $k => $v) {
  39. $temp = [];
  40. $temp['path'] = $v['menu_path'];
  41. $temp['title'] = $v['menu_name'];
  42. $temp['icon'] = $v['icon'];
  43. $temp['header'] = $v['header'];
  44. $temp['is_header'] = $v['is_header'];
  45. if ($v['is_show_path']) {
  46. $temp['auth'] = ['hidden'];
  47. }
  48. if (!empty($v['children'])) {
  49. $temp['children'] = self::toIviewUi($v['children']);
  50. }
  51. $newData[] = $temp;
  52. }
  53. return $newData;
  54. }
  55. /**
  56. * 获取树型菜单
  57. * @param $data
  58. * @param int $pid
  59. * @param int $level
  60. * @return array
  61. */
  62. public static function getTree($data, $pid = 0, $level = 1)
  63. {
  64. $childs = self::getChild($data, $pid, $level);
  65. array_multisort(array_column($childs, 'sort'), SORT_DESC, $childs);
  66. foreach ($childs as $key => $navItem) {
  67. $resChild = self::getTree($data, $navItem['id']);
  68. if (null != $resChild) {
  69. $childs[$key]['children'] = $resChild;
  70. }
  71. }
  72. return $childs;
  73. }
  74. /**
  75. * 获取子菜单
  76. * @param $arr
  77. * @param $id
  78. * @param $lev
  79. * @return array
  80. */
  81. private static function getChild(&$arr, $id, $lev)
  82. {
  83. $child = [];
  84. foreach ($arr as $k => $value) {
  85. if ($value['pid'] == $id) {
  86. $value['level'] = $lev;
  87. $child[] = $value;
  88. }
  89. }
  90. return $child;
  91. }
  92. }