Arr.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. namespace Illuminate\Support;
  3. use ArrayAccess;
  4. use InvalidArgumentException;
  5. use Illuminate\Support\Traits\Macroable;
  6. class Arr
  7. {
  8. use Macroable;
  9. /**
  10. * Determine whether the given value is array accessible.
  11. *
  12. * @param mixed $value
  13. * @return bool
  14. */
  15. public static function accessible($value)
  16. {
  17. return is_array($value) || $value instanceof ArrayAccess;
  18. }
  19. /**
  20. * Add an element to an array using "dot" notation if it doesn't exist.
  21. *
  22. * @param array $array
  23. * @param string $key
  24. * @param mixed $value
  25. * @return array
  26. */
  27. public static function add($array, $key, $value)
  28. {
  29. if (is_null(static::get($array, $key))) {
  30. static::set($array, $key, $value);
  31. }
  32. return $array;
  33. }
  34. /**
  35. * Collapse an array of arrays into a single array.
  36. *
  37. * @param array $array
  38. * @return array
  39. */
  40. public static function collapse($array)
  41. {
  42. $results = [];
  43. foreach ($array as $values) {
  44. if ($values instanceof Collection) {
  45. $values = $values->all();
  46. } elseif (! is_array($values)) {
  47. continue;
  48. }
  49. $results = array_merge($results, $values);
  50. }
  51. return $results;
  52. }
  53. /**
  54. * Cross join the given arrays, returning all possible permutations.
  55. *
  56. * @param array ...$arrays
  57. * @return array
  58. */
  59. public static function crossJoin(...$arrays)
  60. {
  61. $results = [[]];
  62. foreach ($arrays as $index => $array) {
  63. $append = [];
  64. foreach ($results as $product) {
  65. foreach ($array as $item) {
  66. $product[$index] = $item;
  67. $append[] = $product;
  68. }
  69. }
  70. $results = $append;
  71. }
  72. return $results;
  73. }
  74. /**
  75. * Divide an array into two arrays. One with keys and the other with values.
  76. *
  77. * @param array $array
  78. * @return array
  79. */
  80. public static function divide($array)
  81. {
  82. return [array_keys($array), array_values($array)];
  83. }
  84. /**
  85. * Flatten a multi-dimensional associative array with dots.
  86. *
  87. * @param array $array
  88. * @param string $prepend
  89. * @return array
  90. */
  91. public static function dot($array, $prepend = '')
  92. {
  93. $results = [];
  94. foreach ($array as $key => $value) {
  95. if (is_array($value) && ! empty($value)) {
  96. $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
  97. } else {
  98. $results[$prepend.$key] = $value;
  99. }
  100. }
  101. return $results;
  102. }
  103. /**
  104. * Get all of the given array except for a specified array of keys.
  105. *
  106. * @param array $array
  107. * @param array|string $keys
  108. * @return array
  109. */
  110. public static function except($array, $keys)
  111. {
  112. static::forget($array, $keys);
  113. return $array;
  114. }
  115. /**
  116. * Determine if the given key exists in the provided array.
  117. *
  118. * @param \ArrayAccess|array $array
  119. * @param string|int $key
  120. * @return bool
  121. */
  122. public static function exists($array, $key)
  123. {
  124. if ($array instanceof ArrayAccess) {
  125. return $array->offsetExists($key);
  126. }
  127. return array_key_exists($key, $array);
  128. }
  129. /**
  130. * Return the first element in an array passing a given truth test.
  131. *
  132. * @param array $array
  133. * @param callable|null $callback
  134. * @param mixed $default
  135. * @return mixed
  136. */
  137. public static function first($array, callable $callback = null, $default = null)
  138. {
  139. if (is_null($callback)) {
  140. if (empty($array)) {
  141. return value($default);
  142. }
  143. foreach ($array as $item) {
  144. return $item;
  145. }
  146. }
  147. foreach ($array as $key => $value) {
  148. if (call_user_func($callback, $value, $key)) {
  149. return $value;
  150. }
  151. }
  152. return value($default);
  153. }
  154. /**
  155. * Return the last element in an array passing a given truth test.
  156. *
  157. * @param array $array
  158. * @param callable|null $callback
  159. * @param mixed $default
  160. * @return mixed
  161. */
  162. public static function last($array, callable $callback = null, $default = null)
  163. {
  164. if (is_null($callback)) {
  165. return empty($array) ? value($default) : end($array);
  166. }
  167. return static::first(array_reverse($array, true), $callback, $default);
  168. }
  169. /**
  170. * Flatten a multi-dimensional array into a single level.
  171. *
  172. * @param array $array
  173. * @param int $depth
  174. * @return array
  175. */
  176. public static function flatten($array, $depth = INF)
  177. {
  178. $result = [];
  179. foreach ($array as $item) {
  180. $item = $item instanceof Collection ? $item->all() : $item;
  181. if (! is_array($item)) {
  182. $result[] = $item;
  183. } elseif ($depth === 1) {
  184. $result = array_merge($result, array_values($item));
  185. } else {
  186. $result = array_merge($result, static::flatten($item, $depth - 1));
  187. }
  188. }
  189. return $result;
  190. }
  191. /**
  192. * Remove one or many array items from a given array using "dot" notation.
  193. *
  194. * @param array $array
  195. * @param array|string $keys
  196. * @return void
  197. */
  198. public static function forget(&$array, $keys)
  199. {
  200. $original = &$array;
  201. $keys = (array) $keys;
  202. if (count($keys) === 0) {
  203. return;
  204. }
  205. foreach ($keys as $key) {
  206. // if the exact key exists in the top-level, remove it
  207. if (static::exists($array, $key)) {
  208. unset($array[$key]);
  209. continue;
  210. }
  211. $parts = explode('.', $key);
  212. // clean up before each pass
  213. $array = &$original;
  214. while (count($parts) > 1) {
  215. $part = array_shift($parts);
  216. if (isset($array[$part]) && is_array($array[$part])) {
  217. $array = &$array[$part];
  218. } else {
  219. continue 2;
  220. }
  221. }
  222. unset($array[array_shift($parts)]);
  223. }
  224. }
  225. /**
  226. * Get an item from an array using "dot" notation.
  227. *
  228. * @param \ArrayAccess|array $array
  229. * @param string $key
  230. * @param mixed $default
  231. * @return mixed
  232. */
  233. public static function get($array, $key, $default = null)
  234. {
  235. if (! static::accessible($array)) {
  236. return value($default);
  237. }
  238. if (is_null($key)) {
  239. return $array;
  240. }
  241. if (static::exists($array, $key)) {
  242. return $array[$key];
  243. }
  244. if (strpos($key, '.') === false) {
  245. return $array[$key] ?? value($default);
  246. }
  247. foreach (explode('.', $key) as $segment) {
  248. if (static::accessible($array) && static::exists($array, $segment)) {
  249. $array = $array[$segment];
  250. } else {
  251. return value($default);
  252. }
  253. }
  254. return $array;
  255. }
  256. /**
  257. * Check if an item or items exist in an array using "dot" notation.
  258. *
  259. * @param \ArrayAccess|array $array
  260. * @param string|array $keys
  261. * @return bool
  262. */
  263. public static function has($array, $keys)
  264. {
  265. if (is_null($keys)) {
  266. return false;
  267. }
  268. $keys = (array) $keys;
  269. if (! $array) {
  270. return false;
  271. }
  272. if ($keys === []) {
  273. return false;
  274. }
  275. foreach ($keys as $key) {
  276. $subKeyArray = $array;
  277. if (static::exists($array, $key)) {
  278. continue;
  279. }
  280. foreach (explode('.', $key) as $segment) {
  281. if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
  282. $subKeyArray = $subKeyArray[$segment];
  283. } else {
  284. return false;
  285. }
  286. }
  287. }
  288. return true;
  289. }
  290. /**
  291. * Determines if an array is associative.
  292. *
  293. * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
  294. *
  295. * @param array $array
  296. * @return bool
  297. */
  298. public static function isAssoc(array $array)
  299. {
  300. $keys = array_keys($array);
  301. return array_keys($keys) !== $keys;
  302. }
  303. /**
  304. * Get a subset of the items from the given array.
  305. *
  306. * @param array $array
  307. * @param array|string $keys
  308. * @return array
  309. */
  310. public static function only($array, $keys)
  311. {
  312. return array_intersect_key($array, array_flip((array) $keys));
  313. }
  314. /**
  315. * Pluck an array of values from an array.
  316. *
  317. * @param array $array
  318. * @param string|array $value
  319. * @param string|array|null $key
  320. * @return array
  321. */
  322. public static function pluck($array, $value, $key = null)
  323. {
  324. $results = [];
  325. list($value, $key) = static::explodePluckParameters($value, $key);
  326. foreach ($array as $item) {
  327. $itemValue = data_get($item, $value);
  328. // If the key is "null", we will just append the value to the array and keep
  329. // looping. Otherwise we will key the array using the value of the key we
  330. // received from the developer. Then we'll return the final array form.
  331. if (is_null($key)) {
  332. $results[] = $itemValue;
  333. } else {
  334. $itemKey = data_get($item, $key);
  335. if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
  336. $itemKey = (string) $itemKey;
  337. }
  338. $results[$itemKey] = $itemValue;
  339. }
  340. }
  341. return $results;
  342. }
  343. /**
  344. * Explode the "value" and "key" arguments passed to "pluck".
  345. *
  346. * @param string|array $value
  347. * @param string|array|null $key
  348. * @return array
  349. */
  350. protected static function explodePluckParameters($value, $key)
  351. {
  352. $value = is_string($value) ? explode('.', $value) : $value;
  353. $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
  354. return [$value, $key];
  355. }
  356. /**
  357. * Push an item onto the beginning of an array.
  358. *
  359. * @param array $array
  360. * @param mixed $value
  361. * @param mixed $key
  362. * @return array
  363. */
  364. public static function prepend($array, $value, $key = null)
  365. {
  366. if (is_null($key)) {
  367. array_unshift($array, $value);
  368. } else {
  369. $array = [$key => $value] + $array;
  370. }
  371. return $array;
  372. }
  373. /**
  374. * Get a value from the array, and remove it.
  375. *
  376. * @param array $array
  377. * @param string $key
  378. * @param mixed $default
  379. * @return mixed
  380. */
  381. public static function pull(&$array, $key, $default = null)
  382. {
  383. $value = static::get($array, $key, $default);
  384. static::forget($array, $key);
  385. return $value;
  386. }
  387. /**
  388. * Get one or a specified number of random values from an array.
  389. *
  390. * @param array $array
  391. * @param int|null $number
  392. * @return mixed
  393. *
  394. * @throws \InvalidArgumentException
  395. */
  396. public static function random($array, $number = null)
  397. {
  398. $requested = is_null($number) ? 1 : $number;
  399. $count = count($array);
  400. if ($requested > $count) {
  401. throw new InvalidArgumentException(
  402. "You requested {$requested} items, but there are only {$count} items available."
  403. );
  404. }
  405. if (is_null($number)) {
  406. return $array[array_rand($array)];
  407. }
  408. if ((int) $number === 0) {
  409. return [];
  410. }
  411. $keys = array_rand($array, $number);
  412. $results = [];
  413. foreach ((array) $keys as $key) {
  414. $results[] = $array[$key];
  415. }
  416. return $results;
  417. }
  418. /**
  419. * Set an array item to a given value using "dot" notation.
  420. *
  421. * If no key is given to the method, the entire array will be replaced.
  422. *
  423. * @param array $array
  424. * @param string $key
  425. * @param mixed $value
  426. * @return array
  427. */
  428. public static function set(&$array, $key, $value)
  429. {
  430. if (is_null($key)) {
  431. return $array = $value;
  432. }
  433. $keys = explode('.', $key);
  434. while (count($keys) > 1) {
  435. $key = array_shift($keys);
  436. // If the key doesn't exist at this depth, we will just create an empty array
  437. // to hold the next value, allowing us to create the arrays to hold final
  438. // values at the correct depth. Then we'll keep digging into the array.
  439. if (! isset($array[$key]) || ! is_array($array[$key])) {
  440. $array[$key] = [];
  441. }
  442. $array = &$array[$key];
  443. }
  444. $array[array_shift($keys)] = $value;
  445. return $array;
  446. }
  447. /**
  448. * Shuffle the given array and return the result.
  449. *
  450. * @param array $array
  451. * @param int|null $seed
  452. * @return array
  453. */
  454. public static function shuffle($array, $seed = null)
  455. {
  456. if (is_null($seed)) {
  457. shuffle($array);
  458. } else {
  459. srand($seed);
  460. usort($array, function () {
  461. return rand(-1, 1);
  462. });
  463. }
  464. return $array;
  465. }
  466. /**
  467. * Sort the array using the given callback or "dot" notation.
  468. *
  469. * @param array $array
  470. * @param callable|string|null $callback
  471. * @return array
  472. */
  473. public static function sort($array, $callback = null)
  474. {
  475. return Collection::make($array)->sortBy($callback)->all();
  476. }
  477. /**
  478. * Recursively sort an array by keys and values.
  479. *
  480. * @param array $array
  481. * @return array
  482. */
  483. public static function sortRecursive($array)
  484. {
  485. foreach ($array as &$value) {
  486. if (is_array($value)) {
  487. $value = static::sortRecursive($value);
  488. }
  489. }
  490. if (static::isAssoc($array)) {
  491. ksort($array);
  492. } else {
  493. sort($array);
  494. }
  495. return $array;
  496. }
  497. /**
  498. * Filter the array using the given callback.
  499. *
  500. * @param array $array
  501. * @param callable $callback
  502. * @return array
  503. */
  504. public static function where($array, callable $callback)
  505. {
  506. return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
  507. }
  508. /**
  509. * If the given value is not an array and not null, wrap it in one.
  510. *
  511. * @param mixed $value
  512. * @return array
  513. */
  514. public static function wrap($value)
  515. {
  516. if (is_null($value)) {
  517. return [];
  518. }
  519. return ! is_array($value) ? [$value] : $value;
  520. }
  521. }