Arr.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace qiniu\utils;
  12. /**
  13. * 操作数组帮助类
  14. * Class Arr
  15. * @package crmeb\utils
  16. */
  17. class Arr
  18. {
  19. /**
  20. * 对数组增加默认值
  21. * @param array $keys
  22. * @return array
  23. */
  24. public static function getDefaultValue(array $keys, array $configList = [])
  25. {
  26. $value = [];
  27. foreach ($keys as $val) {
  28. if (is_array($val)) {
  29. $k = $val[0] ?? '';
  30. $v = $val[1] ?? '';
  31. } else {
  32. $k = $val;
  33. $v = '';
  34. }
  35. $value[$k] = $configList[$k] ?? $v;
  36. }
  37. return $value;
  38. }
  39. /**
  40. * 获取ivew菜单列表
  41. * @param array $data
  42. * @return array
  43. */
  44. public static function getMenuIviewList(array $data)
  45. {
  46. return Arr::toIviewUi(Arr::getTree($data));
  47. }
  48. /**
  49. * 转化iviewUi需要的key值
  50. * @param $data
  51. * @return array
  52. */
  53. public static function toIviewUi($data)
  54. {
  55. $newData = [];
  56. foreach ($data as $k => $v) {
  57. $temp = [];
  58. $temp['path'] = $v['menu_path'];
  59. $temp['title'] = $v['menu_name'];
  60. $temp['icon'] = $v['icon'];
  61. if ($v['is_show_path']) {
  62. $temp['auth'] = ['hidden'];
  63. }
  64. if (!empty($v['children'])) {
  65. $temp['children'] = self::toIviewUi($v['children']);
  66. }
  67. if (!empty($v['extend'])) {
  68. $temp['extend'] = $v['extend'];
  69. }
  70. $newData[] = $temp;
  71. }
  72. return $newData;
  73. }
  74. /**
  75. * 获取树型菜单
  76. * @param $data
  77. * @param int $pid
  78. * @param int $level
  79. * @return array
  80. */
  81. public static function getTree($data, $pid = 0, $level = 1)
  82. {
  83. $childs = self::getChild($data, $pid, $level);
  84. $dataSort = array_column($childs, 'sort');
  85. array_multisort($dataSort, SORT_DESC, $childs);
  86. foreach ($childs as $key => $navItem) {
  87. $resChild = self::getTree($data, $navItem['id']);
  88. if (null != $resChild) {
  89. $childs[$key]['children'] = $resChild;
  90. }
  91. }
  92. return $childs;
  93. }
  94. /**
  95. * 获取子菜单
  96. * @param $arr
  97. * @param $id
  98. * @param $lev
  99. * @return array
  100. */
  101. private static function getChild(&$arr, $id, $lev)
  102. {
  103. $child = [];
  104. foreach ($arr as $k => $value) {
  105. if ($value['pid'] == $id) {
  106. $value['level'] = $lev;
  107. $child[] = $value;
  108. }
  109. }
  110. return $child;
  111. }
  112. /**
  113. * 格式化数据
  114. * @param array $array
  115. * @param $value
  116. * @param int $default
  117. * @return mixed
  118. */
  119. public static function setValeTime(array $array, $value, $default = 0)
  120. {
  121. foreach ($array as $item) {
  122. if (!isset($value[$item]))
  123. $value[$item] = $default;
  124. else if (is_string($value[$item]))
  125. $value[$item] = (float)$value[$item];
  126. }
  127. return $value;
  128. }
  129. /**
  130. * 获取二维数组中某个值的集合重新组成数组,并判断数组中的每一项是否为真
  131. * @param array $data
  132. * @param string $filed
  133. * @return array
  134. */
  135. public static function getArrayFilterValeu(array $data, string $filed)
  136. {
  137. return array_filter(array_unique(array_column($data, $filed)), function ($item) {
  138. if ($item) {
  139. return $item;
  140. }
  141. });
  142. }
  143. /**
  144. * 获取二维数组中最大的值
  145. * @param $arr
  146. * @param $field
  147. * @return int|string
  148. */
  149. public static function getArrayMax($arr, $field)
  150. {
  151. $temp = [];
  152. foreach ($arr as $k => $v) {
  153. $temp[] = $v[$field];
  154. }
  155. if (!count($temp)) return 0;
  156. $maxNumber = max($temp);
  157. foreach ($arr as $k => $v) {
  158. if ($maxNumber == $v[$field]) return $k;
  159. }
  160. return 0;
  161. }
  162. /**
  163. * 获取二维数组中最小的值
  164. * @param $arr
  165. * @param $field
  166. * @return int|string
  167. */
  168. public static function getArrayMin($arr, $field)
  169. {
  170. $temp = [];
  171. foreach ($arr as $k => $v) {
  172. $temp[] = $v[$field];
  173. }
  174. if (!count($temp)) return 0;
  175. $minNumber = min($temp);
  176. foreach ($arr as $k => $v) {
  177. if ($minNumber == $v[$field]) return $k;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * 数组转字符串去重复
  183. * @param array $data
  184. * @return false|string[]
  185. */
  186. public static function unique(array $data)
  187. {
  188. return array_unique(explode(',', implode(',', $data)));
  189. }
  190. /**
  191. * 获取数组中去重复过后的指定key值
  192. * @param array $list
  193. * @param string $key
  194. * @return array
  195. */
  196. public static function getUniqueKey(array $list, string $key)
  197. {
  198. return array_unique(array_column($list, $key));
  199. }
  200. /**
  201. * 获取数组钟随机值
  202. * @param array $data
  203. * @return bool|mixed
  204. */
  205. public static function getArrayRandKey(array $data)
  206. {
  207. if (!$data) {
  208. return false;
  209. }
  210. mt_srand();
  211. $mun = rand(0, count($data));
  212. if (!isset($data[$mun])) {
  213. return self::getArrayRandKey($data);
  214. }
  215. return $data[$mun];
  216. }
  217. /**
  218. * 格式化数据
  219. * @param array $list
  220. * @return array
  221. */
  222. public static function formatShipping(array $list)
  223. {
  224. $freeDate = [];
  225. foreach ($list as $item) {
  226. $freeDate[$item['uniqid']][] = $item;
  227. }
  228. $data = [];
  229. foreach ($freeDate as $item) {
  230. $cityIds = [];
  231. $cityId = [];
  232. $p = [];
  233. foreach ($item as $value) {
  234. $cityId[] = $value['city_id'];
  235. $cityIds[] = is_array($value['value']) ? $value['value'] : json_decode($value['value'], true);
  236. unset($value['city_id'], $value['value']);
  237. $p = $value;
  238. }
  239. $p['city_id'] = $cityId;
  240. $p['city_ids'] = $cityIds;
  241. $data[] = $p;
  242. }
  243. return $data;
  244. }
  245. /**
  246. * 过滤字段
  247. * @param $value
  248. * @param array $filter
  249. * @return mixed
  250. */
  251. public static function StrFilterValue($value, array $filter = [])
  252. {
  253. $filter = $filter ?: ['strip_tags', 'addslashes', 'trim', 'htmlspecialchars'];
  254. foreach ($filter as $closure) {
  255. if (function_exists($closure)) {
  256. $value = $closure($value);
  257. }
  258. }
  259. return $value;
  260. }
  261. /**
  262. * 过滤字段
  263. * @param array $data
  264. * @return array
  265. */
  266. public static function filterValue(array $data)
  267. {
  268. foreach ($data as &$item) {
  269. if (is_array($item)) {
  270. $item = self::filterValue($item);
  271. } else {
  272. $item = self::StrFilterValue($item);
  273. }
  274. }
  275. return $data;
  276. }
  277. }