Arr.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Support;
  16. /**
  17. * Array helper from Illuminate\Support\Arr.
  18. */
  19. class Arr
  20. {
  21. /**
  22. * Add an element to an array using "dot" notation if it doesn't exist.
  23. *
  24. * @param array $array
  25. * @param string $key
  26. * @param mixed $value
  27. *
  28. * @return array
  29. */
  30. public static function add(array $array, $key, $value)
  31. {
  32. if (is_null(static::get($array, $key))) {
  33. static::set($array, $key, $value);
  34. }
  35. return $array;
  36. }
  37. /**
  38. * Cross join the given arrays, returning all possible permutations.
  39. *
  40. * @param array ...$arrays
  41. *
  42. * @return array
  43. */
  44. public static function crossJoin(...$arrays)
  45. {
  46. $results = [[]];
  47. foreach ($arrays as $index => $array) {
  48. $append = [];
  49. foreach ($results as $product) {
  50. foreach ($array as $item) {
  51. $product[$index] = $item;
  52. $append[] = $product;
  53. }
  54. }
  55. $results = $append;
  56. }
  57. return $results;
  58. }
  59. /**
  60. * Divide an array into two arrays. One with keys and the other with values.
  61. *
  62. * @param array $array
  63. *
  64. * @return array
  65. */
  66. public static function divide(array $array)
  67. {
  68. return [array_keys($array), array_values($array)];
  69. }
  70. /**
  71. * Flatten a multi-dimensional associative array with dots.
  72. *
  73. * @param array $array
  74. * @param string $prepend
  75. *
  76. * @return array
  77. */
  78. public static function dot(array $array, $prepend = '')
  79. {
  80. $results = [];
  81. foreach ($array as $key => $value) {
  82. if (is_array($value) && !empty($value)) {
  83. $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
  84. } else {
  85. $results[$prepend.$key] = $value;
  86. }
  87. }
  88. return $results;
  89. }
  90. /**
  91. * Get all of the given array except for a specified array of items.
  92. *
  93. * @param array $array
  94. * @param array|string $keys
  95. *
  96. * @return array
  97. */
  98. public static function except(array $array, $keys)
  99. {
  100. static::forget($array, $keys);
  101. return $array;
  102. }
  103. /**
  104. * Determine if the given key exists in the provided array.
  105. *
  106. * @param \ArrayAccess|array $array
  107. * @param string|int $key
  108. *
  109. * @return bool
  110. */
  111. public static function exists(array $array, $key)
  112. {
  113. return array_key_exists($key, $array);
  114. }
  115. /**
  116. * Return the first element in an array passing a given truth test.
  117. *
  118. * @param array $array
  119. * @param callable|null $callback
  120. * @param mixed $default
  121. *
  122. * @return mixed
  123. */
  124. public static function first(array $array, callable $callback = null, $default = null)
  125. {
  126. if (is_null($callback)) {
  127. if (empty($array)) {
  128. return $default;
  129. }
  130. foreach ($array as $item) {
  131. return $item;
  132. }
  133. }
  134. foreach ($array as $key => $value) {
  135. if (call_user_func($callback, $value, $key)) {
  136. return $value;
  137. }
  138. }
  139. return $default;
  140. }
  141. /**
  142. * Return the last element in an array passing a given truth test.
  143. *
  144. * @param array $array
  145. * @param callable|null $callback
  146. * @param mixed $default
  147. *
  148. * @return mixed
  149. */
  150. public static function last(array $array, callable $callback = null, $default = null)
  151. {
  152. if (is_null($callback)) {
  153. return empty($array) ? $default : end($array);
  154. }
  155. return static::first(array_reverse($array, true), $callback, $default);
  156. }
  157. /**
  158. * Flatten a multi-dimensional array into a single level.
  159. *
  160. * @param array $array
  161. * @param int $depth
  162. *
  163. * @return array
  164. */
  165. public static function flatten(array $array, $depth = INF)
  166. {
  167. return array_reduce($array, function ($result, $item) use ($depth) {
  168. $item = $item instanceof Collection ? $item->all() : $item;
  169. if (!is_array($item)) {
  170. return array_merge($result, [$item]);
  171. } elseif (1 === $depth) {
  172. return array_merge($result, array_values($item));
  173. }
  174. return array_merge($result, static::flatten($item, $depth - 1));
  175. }, []);
  176. }
  177. /**
  178. * Remove one or many array items from a given array using "dot" notation.
  179. *
  180. * @param array $array
  181. * @param array|string $keys
  182. */
  183. public static function forget(array &$array, $keys)
  184. {
  185. $original = &$array;
  186. $keys = (array) $keys;
  187. if (0 === count($keys)) {
  188. return;
  189. }
  190. foreach ($keys as $key) {
  191. // if the exact key exists in the top-level, remove it
  192. if (static::exists($array, $key)) {
  193. unset($array[$key]);
  194. continue;
  195. }
  196. $parts = explode('.', $key);
  197. // clean up before each pass
  198. $array = &$original;
  199. while (count($parts) > 1) {
  200. $part = array_shift($parts);
  201. if (isset($array[$part]) && is_array($array[$part])) {
  202. $array = &$array[$part];
  203. } else {
  204. continue 2;
  205. }
  206. }
  207. unset($array[array_shift($parts)]);
  208. }
  209. }
  210. /**
  211. * Get an item from an array using "dot" notation.
  212. *
  213. * @param \ArrayAccess|array $array
  214. * @param string $key
  215. * @param mixed $default
  216. *
  217. * @return mixed
  218. */
  219. public static function get(array $array, $key, $default = null)
  220. {
  221. if (is_null($key)) {
  222. return $array;
  223. }
  224. if (static::exists($array, $key)) {
  225. return $array[$key];
  226. }
  227. foreach (explode('.', $key) as $segment) {
  228. if (static::exists($array, $segment)) {
  229. $array = $array[$segment];
  230. } else {
  231. return $default;
  232. }
  233. }
  234. return $array;
  235. }
  236. /**
  237. * Check if an item or items exist in an array using "dot" notation.
  238. *
  239. * @param \ArrayAccess|array $array
  240. * @param string|array $keys
  241. *
  242. * @return bool
  243. */
  244. public static function has(array $array, $keys)
  245. {
  246. if (is_null($keys)) {
  247. return false;
  248. }
  249. $keys = (array) $keys;
  250. if (empty($array)) {
  251. return false;
  252. }
  253. if ($keys === []) {
  254. return false;
  255. }
  256. foreach ($keys as $key) {
  257. $subKeyArray = $array;
  258. if (static::exists($array, $key)) {
  259. continue;
  260. }
  261. foreach (explode('.', $key) as $segment) {
  262. if (static::exists($subKeyArray, $segment)) {
  263. $subKeyArray = $subKeyArray[$segment];
  264. } else {
  265. return false;
  266. }
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * Determines if an array is associative.
  273. *
  274. * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
  275. *
  276. * @param array $array
  277. *
  278. * @return bool
  279. */
  280. public static function isAssoc(array $array)
  281. {
  282. $keys = array_keys($array);
  283. return array_keys($keys) !== $keys;
  284. }
  285. /**
  286. * Get a subset of the items from the given array.
  287. *
  288. * @param array $array
  289. * @param array|string $keys
  290. *
  291. * @return array
  292. */
  293. public static function only(array $array, $keys)
  294. {
  295. return array_intersect_key($array, array_flip((array) $keys));
  296. }
  297. /**
  298. * Push an item onto the beginning of an array.
  299. *
  300. * @param array $array
  301. * @param mixed $value
  302. * @param mixed $key
  303. *
  304. * @return array
  305. */
  306. public static function prepend(array $array, $value, $key = null)
  307. {
  308. if (is_null($key)) {
  309. array_unshift($array, $value);
  310. } else {
  311. $array = [$key => $value] + $array;
  312. }
  313. return $array;
  314. }
  315. /**
  316. * Get a value from the array, and remove it.
  317. *
  318. * @param array $array
  319. * @param string $key
  320. * @param mixed $default
  321. *
  322. * @return mixed
  323. */
  324. public static function pull(array &$array, $key, $default = null)
  325. {
  326. $value = static::get($array, $key, $default);
  327. static::forget($array, $key);
  328. return $value;
  329. }
  330. /**
  331. * Get a 1 value from an array.
  332. *
  333. * @param array $array
  334. * @param int|null $amount
  335. *
  336. * @throws \InvalidArgumentException
  337. *
  338. * @return mixed
  339. */
  340. public static function random(array $array, int $amount = null)
  341. {
  342. if (is_null($amount)) {
  343. return $array[array_rand($array)];
  344. }
  345. $keys = array_rand($array, $amount);
  346. $results = [];
  347. foreach ((array) $keys as $key) {
  348. $results[] = $array[$key];
  349. }
  350. return $results;
  351. }
  352. /**
  353. * Set an array item to a given value using "dot" notation.
  354. *
  355. * If no key is given to the method, the entire array will be replaced.
  356. *
  357. * @param array $array
  358. * @param string $key
  359. * @param mixed $value
  360. *
  361. * @return array
  362. */
  363. public static function set(array &$array, string $key, $value)
  364. {
  365. $keys = explode('.', $key);
  366. while (count($keys) > 1) {
  367. $key = array_shift($keys);
  368. // If the key doesn't exist at this depth, we will just create an empty array
  369. // to hold the next value, allowing us to create the arrays to hold final
  370. // values at the correct depth. Then we'll keep digging into the array.
  371. if (!isset($array[$key]) || !is_array($array[$key])) {
  372. $array[$key] = [];
  373. }
  374. $array = &$array[$key];
  375. }
  376. $array[array_shift($keys)] = $value;
  377. return $array;
  378. }
  379. /**
  380. * Filter the array using the given callback.
  381. *
  382. * @param array $array
  383. * @param callable $callback
  384. *
  385. * @return array
  386. */
  387. public static function where(array $array, callable $callback)
  388. {
  389. return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
  390. }
  391. /**
  392. * If the given value is not an array, wrap it in one.
  393. *
  394. * @param mixed $value
  395. *
  396. * @return array
  397. */
  398. public static function wrap($value)
  399. {
  400. return !is_array($value) ? [$value] : $value;
  401. }
  402. }