Arr.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 crmeb\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. $temp['header'] = $v['header'];
  62. $temp['is_header'] = $v['is_header'];
  63. if ($v['is_show_path']) {
  64. $temp['auth'] = ['hidden'];
  65. }
  66. if (!empty($v['children'])) {
  67. $temp['children'] = self::toIviewUi($v['children']);
  68. }
  69. $newData[] = $temp;
  70. }
  71. return $newData;
  72. }
  73. /**
  74. * 获取树型菜单
  75. * @param $data
  76. * @param int $pid
  77. * @param int $level
  78. * @return array
  79. */
  80. public static function getTree($data, $pid = 0, $level = 1)
  81. {
  82. $childs = self::getChild($data, $pid, $level);
  83. $dataSort = array_column($childs, 'sort');
  84. array_multisort($dataSort, SORT_DESC, $childs);
  85. foreach ($childs as $key => $navItem) {
  86. $resChild = self::getTree($data, $navItem['id']);
  87. if (null != $resChild) {
  88. $childs[$key]['children'] = $resChild;
  89. }
  90. }
  91. return $childs;
  92. }
  93. /**
  94. * 获取子菜单
  95. * @param $arr
  96. * @param $id
  97. * @param $lev
  98. * @return array
  99. */
  100. private static function getChild(&$arr, $id, $lev)
  101. {
  102. $child = [];
  103. foreach ($arr as $k => $value) {
  104. if ($value['pid'] == $id) {
  105. $value['level'] = $lev;
  106. $child[] = $value;
  107. }
  108. }
  109. return $child;
  110. }
  111. /**
  112. * 格式化数据
  113. * @param array $array
  114. * @param $value
  115. * @param int $default
  116. * @return mixed
  117. */
  118. public static function setValeTime(array $array, $value, $default = 0)
  119. {
  120. foreach ($array as $item) {
  121. if (!isset($value[$item]))
  122. $value[$item] = $default;
  123. else if (is_string($value[$item]))
  124. $value[$item] = (float)$value[$item];
  125. }
  126. return $value;
  127. }
  128. /**
  129. * 获取二维数组中某个值的集合重新组成数组,并判断数组中的每一项是否为真
  130. * @param array $data
  131. * @param string $filed
  132. * @return array
  133. */
  134. public static function getArrayFilterValeu(array $data, string $filed)
  135. {
  136. return array_filter(array_unique(array_column($data, $filed)), function ($item) {
  137. if ($item) {
  138. return $item;
  139. }
  140. });
  141. }
  142. /**
  143. * 获取二维数组中最大的值
  144. * @param $arr
  145. * @param $field
  146. * @return int|string
  147. */
  148. public static function getArrayMax($arr, $field)
  149. {
  150. $temp = [];
  151. foreach ($arr as $k => $v) {
  152. $temp[] = $v[$field];
  153. }
  154. if (!count($temp)) return 0;
  155. $maxNumber = max($temp);
  156. foreach ($arr as $k => $v) {
  157. if ($maxNumber == $v[$field]) return $k;
  158. }
  159. return 0;
  160. }
  161. /**
  162. * 获取二维数组中最小的值
  163. * @param $arr
  164. * @param $field
  165. * @return int|string
  166. */
  167. public static function getArrayMin($arr, $field)
  168. {
  169. $temp = [];
  170. foreach ($arr as $k => $v) {
  171. $temp[] = $v[$field];
  172. }
  173. if (!count($temp)) return 0;
  174. $minNumber = min($temp);
  175. foreach ($arr as $k => $v) {
  176. if ($minNumber == $v[$field]) return $k;
  177. }
  178. return 0;
  179. }
  180. /**
  181. * 数组转字符串去重复
  182. * @param array $data
  183. * @return false|string[]
  184. */
  185. public static function unique(array $data)
  186. {
  187. return array_unique(explode(',', implode(',', $data)));
  188. }
  189. /**
  190. * 获取数组中去重复过后的指定key值
  191. * @param array $list
  192. * @param string $key
  193. * @return array
  194. */
  195. public static function getUniqueKey(array $list, string $key)
  196. {
  197. return array_unique(array_column($list, $key));
  198. }
  199. /**
  200. * 获取数组钟随机值
  201. * @param array $data
  202. * @return bool|mixed
  203. */
  204. public static function getArrayRandKey(array $data)
  205. {
  206. if (!$data) {
  207. return false;
  208. }
  209. mt_srand();
  210. $mun = rand(0, count($data));
  211. if (!isset($data[$mun])) {
  212. return self::getArrayRandKey($data);
  213. }
  214. return $data[$mun];
  215. }
  216. /**
  217. * 格式化数据
  218. * @param array $list
  219. * @return array
  220. */
  221. public static function formatShipping(array $list)
  222. {
  223. $freeDate = [];
  224. foreach ($list as $item) {
  225. $freeDate[$item['uniqid']][] = $item;
  226. }
  227. $data = [];
  228. foreach ($freeDate as $item) {
  229. $cityIds = [];
  230. $cityId = [];
  231. $p = [];
  232. foreach ($item as $value) {
  233. $cityId[] = $value['city_id'];
  234. $cityIds[] = is_array($value['value']) ? $value['value'] : json_decode($value['value'], true);
  235. unset($value['city_id'], $value['value']);
  236. $p = $value;
  237. }
  238. $p['city_id'] = $cityId;
  239. $p['city_ids'] = $cityIds;
  240. $data[] = $p;
  241. }
  242. return $data;
  243. }
  244. /**
  245. * 过滤字段
  246. * @param $value
  247. * @param array $filter
  248. * @return mixed
  249. */
  250. public static function StrFilterValue($value, array $filter = [])
  251. {
  252. $filter = $filter ?: ['strip_tags', 'addslashes', 'trim', 'htmlspecialchars'];
  253. foreach ($filter as $closure) {
  254. if (function_exists($closure)) {
  255. $value = $closure($value);
  256. }
  257. }
  258. return $value;
  259. }
  260. /**
  261. * 过滤字段
  262. * @param array $data
  263. * @return array
  264. */
  265. public static function filterValue(array $data)
  266. {
  267. foreach ($data as &$item) {
  268. if (is_array($item)) {
  269. $item = self::filterValue($item);
  270. } else {
  271. $item = self::StrFilterValue($item);
  272. }
  273. }
  274. return $data;
  275. }
  276. }