Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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. $keys = (array) $keys;
  266. if (! $array || $keys === []) {
  267. return false;
  268. }
  269. foreach ($keys as $key) {
  270. $subKeyArray = $array;
  271. if (static::exists($array, $key)) {
  272. continue;
  273. }
  274. foreach (explode('.', $key) as $segment) {
  275. if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
  276. $subKeyArray = $subKeyArray[$segment];
  277. } else {
  278. return false;
  279. }
  280. }
  281. }
  282. return true;
  283. }
  284. /**
  285. * Determines if an array is associative.
  286. *
  287. * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
  288. *
  289. * @param array $array
  290. * @return bool
  291. */
  292. public static function isAssoc(array $array)
  293. {
  294. $keys = array_keys($array);
  295. return array_keys($keys) !== $keys;
  296. }
  297. /**
  298. * Get a subset of the items from the given array.
  299. *
  300. * @param array $array
  301. * @param array|string $keys
  302. * @return array
  303. */
  304. public static function only($array, $keys)
  305. {
  306. return array_intersect_key($array, array_flip((array) $keys));
  307. }
  308. /**
  309. * Pluck an array of values from an array.
  310. *
  311. * @param array $array
  312. * @param string|array $value
  313. * @param string|array|null $key
  314. * @return array
  315. */
  316. public static function pluck($array, $value, $key = null)
  317. {
  318. $results = [];
  319. [$value, $key] = static::explodePluckParameters($value, $key);
  320. foreach ($array as $item) {
  321. $itemValue = data_get($item, $value);
  322. // If the key is "null", we will just append the value to the array and keep
  323. // looping. Otherwise we will key the array using the value of the key we
  324. // received from the developer. Then we'll return the final array form.
  325. if (is_null($key)) {
  326. $results[] = $itemValue;
  327. } else {
  328. $itemKey = data_get($item, $key);
  329. if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
  330. $itemKey = (string) $itemKey;
  331. }
  332. $results[$itemKey] = $itemValue;
  333. }
  334. }
  335. return $results;
  336. }
  337. /**
  338. * Explode the "value" and "key" arguments passed to "pluck".
  339. *
  340. * @param string|array $value
  341. * @param string|array|null $key
  342. * @return array
  343. */
  344. protected static function explodePluckParameters($value, $key)
  345. {
  346. $value = is_string($value) ? explode('.', $value) : $value;
  347. $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
  348. return [$value, $key];
  349. }
  350. /**
  351. * Push an item onto the beginning of an array.
  352. *
  353. * @param array $array
  354. * @param mixed $value
  355. * @param mixed $key
  356. * @return array
  357. */
  358. public static function prepend($array, $value, $key = null)
  359. {
  360. if (is_null($key)) {
  361. array_unshift($array, $value);
  362. } else {
  363. $array = [$key => $value] + $array;
  364. }
  365. return $array;
  366. }
  367. /**
  368. * Get a value from the array, and remove it.
  369. *
  370. * @param array $array
  371. * @param string $key
  372. * @param mixed $default
  373. * @return mixed
  374. */
  375. public static function pull(&$array, $key, $default = null)
  376. {
  377. $value = static::get($array, $key, $default);
  378. static::forget($array, $key);
  379. return $value;
  380. }
  381. /**
  382. * Get one or a specified number of random values from an array.
  383. *
  384. * @param array $array
  385. * @param int|null $number
  386. * @return mixed
  387. *
  388. * @throws \InvalidArgumentException
  389. */
  390. public static function random($array, $number = null)
  391. {
  392. $requested = is_null($number) ? 1 : $number;
  393. $count = count($array);
  394. if ($requested > $count) {
  395. throw new InvalidArgumentException(
  396. "You requested {$requested} items, but there are only {$count} items available."
  397. );
  398. }
  399. if (is_null($number)) {
  400. return $array[array_rand($array)];
  401. }
  402. if ((int) $number === 0) {
  403. return [];
  404. }
  405. $keys = array_rand($array, $number);
  406. $results = [];
  407. foreach ((array) $keys as $key) {
  408. $results[] = $array[$key];
  409. }
  410. return $results;
  411. }
  412. /**
  413. * Set an array item to a given value using "dot" notation.
  414. *
  415. * If no key is given to the method, the entire array will be replaced.
  416. *
  417. * @param array $array
  418. * @param string $key
  419. * @param mixed $value
  420. * @return array
  421. */
  422. public static function set(&$array, $key, $value)
  423. {
  424. if (is_null($key)) {
  425. return $array = $value;
  426. }
  427. $keys = explode('.', $key);
  428. while (count($keys) > 1) {
  429. $key = array_shift($keys);
  430. // If the key doesn't exist at this depth, we will just create an empty array
  431. // to hold the next value, allowing us to create the arrays to hold final
  432. // values at the correct depth. Then we'll keep digging into the array.
  433. if (! isset($array[$key]) || ! is_array($array[$key])) {
  434. $array[$key] = [];
  435. }
  436. $array = &$array[$key];
  437. }
  438. $array[array_shift($keys)] = $value;
  439. return $array;
  440. }
  441. /**
  442. * Shuffle the given array and return the result.
  443. *
  444. * @param array $array
  445. * @param int|null $seed
  446. * @return array
  447. */
  448. public static function shuffle($array, $seed = null)
  449. {
  450. if (is_null($seed)) {
  451. shuffle($array);
  452. } else {
  453. mt_srand($seed);
  454. shuffle($array);
  455. mt_srand();
  456. }
  457. return $array;
  458. }
  459. /**
  460. * Sort the array using the given callback or "dot" notation.
  461. *
  462. * @param array $array
  463. * @param callable|string|null $callback
  464. * @return array
  465. */
  466. public static function sort($array, $callback = null)
  467. {
  468. return Collection::make($array)->sortBy($callback)->all();
  469. }
  470. /**
  471. * Recursively sort an array by keys and values.
  472. *
  473. * @param array $array
  474. * @return array
  475. */
  476. public static function sortRecursive($array)
  477. {
  478. foreach ($array as &$value) {
  479. if (is_array($value)) {
  480. $value = static::sortRecursive($value);
  481. }
  482. }
  483. if (static::isAssoc($array)) {
  484. ksort($array);
  485. } else {
  486. sort($array);
  487. }
  488. return $array;
  489. }
  490. /**
  491. * Convert the array into a query string.
  492. *
  493. * @param array $array
  494. * @return string
  495. */
  496. public static function query($array)
  497. {
  498. return http_build_query($array, null, '&', PHP_QUERY_RFC3986);
  499. }
  500. /**
  501. * Filter the array using the given callback.
  502. *
  503. * @param array $array
  504. * @param callable $callback
  505. * @return array
  506. */
  507. public static function where($array, callable $callback)
  508. {
  509. return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
  510. }
  511. /**
  512. * If the given value is not an array and not null, wrap it in one.
  513. *
  514. * @param mixed $value
  515. * @return array
  516. */
  517. public static function wrap($value)
  518. {
  519. if (is_null($value)) {
  520. return [];
  521. }
  522. return is_array($value) ? $value : [$value];
  523. }
  524. }