ArrayHelper.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace common\helpers;
  3. use yii\helpers\BaseArrayHelper;
  4. use yii\helpers\Json;
  5. /**
  6. * Class ArrayHelper
  7. * @package common\helpers
  8. * @author qimall
  9. */
  10. class ArrayHelper extends BaseArrayHelper
  11. {
  12. /**
  13. * 递归数组
  14. *
  15. * @param array $items
  16. * @param string $idField
  17. * @param int $pid
  18. * @param string $pidField
  19. * @return array
  20. */
  21. public static function itemsMerge(array $items, $pid = 0, $idField = "id", $pidField = 'pid', $child = '-')
  22. {
  23. $map = [];
  24. $tree = [];
  25. foreach ($items as &$it) {
  26. $it[$child] = [];
  27. $map[$it[$idField]] = &$it;
  28. }
  29. foreach ($items as &$it) {
  30. $parent = &$map[$it[$pidField]];
  31. if ($parent) {
  32. $parent[$child][] = &$it;
  33. } else {
  34. $pid == $it[$pidField] && $tree[] = &$it;
  35. }
  36. }
  37. unset($items, $map);
  38. return $tree;
  39. }
  40. /**
  41. * 传递一个子分类ID返回所有的父级分类
  42. *
  43. * @param array $items
  44. * @param $id
  45. * @return array
  46. */
  47. public static function getParents(array $items, $id)
  48. {
  49. $arr = [];
  50. foreach ($items as $v) {
  51. if ($v['id'] == $id) {
  52. $arr[] = $v;
  53. $arr = array_merge(self::getParents($items, $v['pid']), $arr);
  54. }
  55. }
  56. return $arr;
  57. }
  58. /**
  59. * 传递一个父级分类ID返回所有子分类
  60. *
  61. * @param $cate
  62. * @param int $pid
  63. * @return array
  64. */
  65. public static function getChilds($cate, $pid)
  66. {
  67. $arr = [];
  68. foreach ($cate as $v) {
  69. if ($v['pid'] == $pid) {
  70. $arr[] = $v;
  71. $arr = array_merge($arr, self::getChilds($cate, $v['id']));
  72. }
  73. }
  74. return $arr;
  75. }
  76. /**
  77. * 传递一个父级分类ID返回所有子分类ID
  78. *
  79. * @param $cate
  80. * @param $pid
  81. * @param string $idField
  82. * @param string $pidField
  83. * @return array
  84. */
  85. public static function getChildIds($cate, $pid, $idField = "id", $pidField = 'pid')
  86. {
  87. $arr = [];
  88. foreach ($cate as $v) {
  89. if ($v[$pidField] == $pid) {
  90. $arr[] = $v[$idField];
  91. $arr = array_merge($arr, self::getChildIds($cate, $v[$idField], $idField, $pidField));
  92. }
  93. }
  94. return $arr;
  95. }
  96. /**
  97. * php二维数组排序 按照指定的key 对数组进行排序
  98. *
  99. * @param array $arr 将要排序的数组
  100. * @param string $keys 指定排序的key
  101. * @param string $type 排序类型 asc | desc
  102. * @return array
  103. */
  104. public static function arraySort($arr, $keys, $type = 'asc')
  105. {
  106. if (count($arr) <= 1) {
  107. return $arr;
  108. }
  109. $keysValue = [];
  110. $newArray = [];
  111. foreach ($arr as $k => $v) {
  112. $keysValue[$k] = $v[$keys];
  113. }
  114. $type == 'asc' ? asort($keysValue) : arsort($keysValue);
  115. reset($keysValue);
  116. foreach ($keysValue as $k => $v) {
  117. $newArray[$k] = $arr[$k];
  118. }
  119. return $newArray;
  120. }
  121. public static function arraySorts($arr, $keys, $type = 'asc')
  122. {
  123. if (count($arr) <= 1) {
  124. return $arr;
  125. }
  126. $keysValue = [];
  127. $newArray = [];
  128. foreach ($arr as $k => $v) {
  129. $keysValue[$k] = $v[$keys];
  130. }
  131. $type == 'asc' ? asort($keysValue) : arsort($keysValue);
  132. reset($keysValue);
  133. foreach ($keysValue as $k => $v) {
  134. $newArray[] = $arr[$k];
  135. }
  136. return $newArray;
  137. }
  138. /**
  139. * 获取数组指定的字段为key
  140. *
  141. * @param array $arr 数组
  142. * @param string $field 要成为key的字段名
  143. * @return array
  144. */
  145. public static function arrayKey($arr, $field)
  146. {
  147. $newArray = [];
  148. if (empty($arr)) {
  149. return $newArray;
  150. }
  151. foreach ($arr as $value) {
  152. isset($value[$field]) && $newArray[$value[$field]] = $value;
  153. }
  154. return $newArray;
  155. }
  156. /**
  157. * 移除数组内某个key的值为传递的值
  158. *
  159. * @param array $array
  160. * @param $value
  161. * @param string $key
  162. * @return array
  163. */
  164. public static function removeByValue(array $array, $value, $key = 'id')
  165. {
  166. foreach ($array as $index => $item) {
  167. if ($item[$key] == $value) {
  168. unset($array[$index]);
  169. }
  170. }
  171. return $array;
  172. }
  173. /**
  174. * 获取数字区间
  175. *
  176. * @param int $start
  177. * @param int $end
  178. * @return array
  179. */
  180. public static function numBetween($start = 0, $end = 1, $key = true, $step_number = 1)
  181. {
  182. $arr = [];
  183. for ($i = $start; $i <= $end; $i = $i + $step_number) {
  184. $key == true ? $arr[$i] = $i : $arr[] = $i;
  185. }
  186. return $arr;
  187. }
  188. /**
  189. * 根据级别和数组返回字符串
  190. *
  191. * @param int $level 级别
  192. * @param array $models
  193. * @param $k
  194. * @param int $treeStat 开始计算
  195. * @return bool|string
  196. */
  197. public static function itemsLevel($level, array $models, $k, $treeStat = 1)
  198. {
  199. $str = '';
  200. for ($i = 1; $i < $level; $i++) {
  201. $str .= '  ';
  202. if ($i == $level - $treeStat) {
  203. if (isset($models[$k + 1])) {
  204. return $str . "├──";
  205. }
  206. return $str . "└──";
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * 必须经过递归才能进行重组为下拉框
  213. *
  214. * @param $models
  215. * @param string $idField
  216. * @param string $titleField
  217. * @param int $treeStat
  218. * @return array
  219. */
  220. public static function itemsMergeDropDown($models, $idField = 'id', $titleField = 'title', $treeStat = 1)
  221. {
  222. $arr = [];
  223. foreach ($models as $k => $model) {
  224. $arr[] = [
  225. $idField => $model[$idField],
  226. $titleField => self::itemsLevel($model['level'], $models, $k, $treeStat) . " " . $model[$titleField],
  227. ];
  228. if (!empty($model['-'])) {
  229. $arr = ArrayHelper::merge(
  230. $arr,
  231. self::itemsMergeDropDown($model['-'], $idField, $titleField, $treeStat)
  232. );
  233. }
  234. }
  235. return $arr;
  236. }
  237. /**
  238. * 匹配ip在ip数组内支持通配符
  239. *
  240. * @param $ip
  241. * @param $allowedIPs
  242. * @return bool
  243. */
  244. public static function ipInArray($ip, $allowedIPs)
  245. {
  246. foreach ($allowedIPs as $filter) {
  247. if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. /**
  254. * 对比2组id,返回存在的id和被删除的id
  255. *
  256. * @param array $oldIds
  257. * @param array $newIds
  258. * @return array
  259. */
  260. public static function comparisonIds(array $oldIds, array $newIds)
  261. {
  262. $updatedIds = $deleteIds = [];
  263. foreach ($oldIds as $oldId) {
  264. if (in_array($oldId, $newIds)) {
  265. $updatedIds[] = $oldId;
  266. } else {
  267. $deleteIds[] = $oldId;
  268. }
  269. }
  270. return [$updatedIds, $deleteIds];
  271. }
  272. /**
  273. * 获取递归的第一个没有子级的数据
  274. *
  275. * @param $array
  276. * @return mixed
  277. */
  278. public static function getFirstRowByItemsMerge(array $array)
  279. {
  280. foreach ($array as $item) {
  281. if (!empty($item['-'])) {
  282. return self::getFirstRowByItemsMerge($item['-']);
  283. } else {
  284. return $item;
  285. }
  286. }
  287. return false;
  288. }
  289. /**
  290. * 获取所有没有子级的数据
  291. *
  292. * @param $array
  293. * @return mixed
  294. */
  295. public static function getNotChildRowsByItemsMerge(array $array)
  296. {
  297. $arr = [];
  298. foreach ($array as $item) {
  299. if (!empty($item['-'])) {
  300. $arr = array_merge($arr, self::getNotChildRowsByItemsMerge($item['-']));
  301. } else {
  302. $arr[] = $item;
  303. }
  304. }
  305. return $arr;
  306. }
  307. /**
  308. * 递归转普通二维数组
  309. *
  310. * @param $array
  311. * @return mixed
  312. */
  313. public static function getRowsByItemsMerge(array $array, $childField = '-')
  314. {
  315. $arr = [];
  316. foreach ($array as $item) {
  317. if (!empty($item[$childField])) {
  318. $arr = array_merge($arr, self::getRowsByItemsMerge($item[$childField]));
  319. }
  320. unset($item[$childField]);
  321. $arr[] = $item;
  322. }
  323. return $arr;
  324. }
  325. /**
  326. * 重组 map 类型转为正常的数组
  327. *
  328. * @param array $array
  329. * @param string $keyForField
  330. * @param string $valueForField
  331. * @return array
  332. */
  333. public static function regroupMapToArr($array = [], $keyForField = 'route', $valueForField = 'title')
  334. {
  335. $arr = [];
  336. foreach ($array as $key => $item) {
  337. if (!is_array($array[$key])) {
  338. $arr[] = [
  339. $keyForField => $key,
  340. $valueForField => $item,
  341. ];
  342. } else {
  343. $arr[] = $item;
  344. }
  345. }
  346. return $arr;
  347. }
  348. /**
  349. * 数组内某字段转数组
  350. *
  351. * @param array $data
  352. * @param string $field
  353. * @return array
  354. */
  355. public static function fieldToArray(array $data, $field = 'covers')
  356. {
  357. foreach ($data as &$datum) {
  358. if (empty($datum[$field])) {
  359. $datum[$field] = [];
  360. }
  361. if (!is_array($datum[$field])) {
  362. $datum[$field] = Json::decode($datum[$field]);
  363. }
  364. }
  365. return $data;
  366. }
  367. /**
  368. * 数组转xml
  369. *
  370. *
  371. * @param $arr
  372. * 微信回调成功:['return_code' => 'SUCCESS', 'return_msg' => 'OK']
  373. * 微信回调失败:['return_code' => 'FAIL', 'return_msg' => 'OK']
  374. * @return bool|string
  375. */
  376. public static function toXml($arr)
  377. {
  378. if (!is_array($arr) || count($arr) <= 0) {
  379. return false;
  380. }
  381. $xml = "<xml>";
  382. foreach ($arr as $key => $val) {
  383. if (is_numeric($val)) {
  384. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  385. } else {
  386. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  387. }
  388. }
  389. $xml .= "</xml>";
  390. return $xml;
  391. }
  392. public static function get_client_ip()
  393. {
  394. if (!empty($_SERVER['REMOTE_ADDR'])) {
  395. $ip = $_SERVER['REMOTE_ADDR'];
  396. } else {
  397. // for php-cli(phpunit etc.)
  398. $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
  399. }
  400. return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
  401. }
  402. /**
  403. * 排列组合
  404. * 采用二进制方法进行组合的选择,如表示5选3时,只需有3位为1就可以了,所以可得到的组合是 01101 11100 00111 10011 01110等10种组合
  405. *
  406. * @param array $arr 需要排列的数组
  407. * @param integer $min_size 最小个数
  408. * @return array 满足条件的新数组组合
  409. */
  410. public static function pl($arr, $size = 5)
  411. {
  412. $len = count($arr);
  413. $max = pow(2, $len);
  414. $min = pow(2, $size) - 1;
  415. $r_arr = array();
  416. for ($i = $min; $i < $max; $i++) {
  417. $count = 0;
  418. $t_arr = array();
  419. for ($j = 0; $j < $len; $j++) {
  420. $a = pow(2, $j);
  421. $t = $i & $a;
  422. if ($t == $a) {
  423. $t_arr[] = $arr[$j];
  424. $count++;
  425. }
  426. }
  427. if ($count == $size) {
  428. $r_arr[] = $t_arr;
  429. }
  430. }
  431. return $r_arr;
  432. }
  433. /**
  434. * N个数组的笛卡尔集
  435. * @Author bing
  436. * @DateTime 2021-09-03 15:06:19
  437. * @copyright: Copyright (c) 2020 广东七件事集团
  438. * @return array
  439. */
  440. public static function combineDika()
  441. {
  442. $data = func_get_args();
  443. $data = current($data);
  444. $result = array();
  445. $arr1 = array_shift($data);
  446. foreach ($arr1 as $key => $item) {
  447. $result[] = array($item);
  448. }
  449. foreach ($data as $key => $item) {
  450. $result = self::combineArray($result, $item);
  451. }
  452. return $result;
  453. }
  454. /**
  455. * 两个数组的笛卡尔积
  456. * @Author bing
  457. * @DateTime 2021-09-03 15:07:11
  458. * @copyright: Copyright (c) 2020 广东七件事集团
  459. * @param [type] $arr1
  460. * @param [type] $arr2
  461. * @return array
  462. */
  463. public static function combineArray($arr1, $arr2)
  464. {
  465. $result = array();
  466. foreach ($arr1 as $item1) {
  467. foreach ($arr2 as $item2) {
  468. $temp = $item1;
  469. $temp[] = $item2;
  470. $result[] = $temp;
  471. }
  472. }
  473. return $result;
  474. }
  475. /**
  476. * 二维数组组合为树形结构
  477. * @Author: lun
  478. * @DateTime: 2023/8/4 0004 10:13
  479. * @Copyright: copyright (c) 2021 广东七件事集团
  480. * @param array $arrays 数组
  481. * @param string $idField 子字段
  482. * @param string $pidField 父字段
  483. * @param string $child 组合的子参数字段
  484. * @return array
  485. */
  486. public static function buildTree($arrays, $idField = "id", $pidField = 'pid', $child = '-') {
  487. $tree = array();
  488. $isset_key = [];
  489. foreach ($arrays as $item) {
  490. $tree[$item[$idField]] = $item;
  491. }
  492. foreach ($tree as $key => $item) {
  493. if ($item[$pidField] && isset($tree[$item[$pidField]])) {
  494. $isset_key[] = $key;
  495. $tree[$item[$pidField]][$child][] = &$tree[$key];
  496. }
  497. }
  498. foreach ($tree as $key => $item) {
  499. if (in_array($key, $isset_key)) unset($tree[$key]);
  500. if ($item[$pidField] && isset($tree[$item[$pidField]])) {
  501. unset($tree[$key]);
  502. }
  503. }
  504. return $tree;
  505. }
  506. /**
  507. * 去除重复项
  508. * @param array $list
  509. * @param $field
  510. * @return array
  511. */
  512. public static function removeDuplicates($list, $field)
  513. {
  514. if (!empty($list)) {
  515. $tmp = [];
  516. $list = (array) $list;
  517. foreach ($list as $key => $item) {
  518. if (is_array($item) && isset($item[$field])) {
  519. if (in_array($item[$field], $tmp)) {
  520. unset($list[$key]);
  521. } else {
  522. $tmp[] = $item[$field];
  523. }
  524. }
  525. }
  526. }
  527. return $list;
  528. }
  529. /**
  530. * 数组查找
  531. *
  532. * @param array $array
  533. * @param \Closure $callback
  534. * @return mixed
  535. */
  536. public static function find(array $array, \Closure $callback)
  537. {
  538. $result = array_filter($array, $callback);
  539. return reset($result); // 返回匹配到的第一个元素
  540. }
  541. }