Arr.php 2.8 KB

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