Arr
class Arr
Contains functions that support array operations.
Methods
Add a value to an array if the key does not exist.
Split an array into two separate arrays: one with keys, one with values.
Pluck a nested value from an array.
Shuffle an associative array while preserving keys.
Collapse a multi-dimensional array into a single-level array.
Split an array into chunks of a given size.
Chunk an array into groups based on a callback function.
Determine if a given value exists in an array.
Compute the Cartesian product of multiple arrays.
Recursively merge two or more arrays.
Recursively compute the difference between two arrays with keys.
Convert a multi-dimensional array into dot notation keys.
Get all items except the specified keys.
Check if a key exists in an array (non-dot notation).
Fill an array with a specified value.
Filter an array using a callback function.
Filter an array to include only the specified keys.
Filter an array by its values.
Get the first element that passes a given test.
Flatten a multi-dimensional array into a single level.
Flatten an array up to a specified depth.
Convert a multi-dimensional array into dot notation keys.
Remove a value from an array using dot notation.
Get a value from an array using dot notation.
Group an array by a given key.
Check if an array has a given key using dot notation.
Check if all given keys exist in the array.
Check if at least one key exists in the array.
Determine if at least one of the given keys exists in the array.
Insert an element before a given key in an array.
Insert an element after a given key in an array.
Determine if a given value is an array.
Determine if an array is associative (i.e., contains at least one non-numeric key).
Check if the given array is empty.
Check if the given array is not empty.
Get all the keys from an array.
Reindex an array using a specified key.
Get the last element that passes a given test.
Apply a callback to each item in an array.
Map an array while preserving keys.
Merge one or more arrays together.
Get only the specified keys from an array.
Partition an array into two arrays: One where the callback returns true, the other where it returns false.
Pluck a single key from an array.
Prepend a value to an array.
Retrieve a value from the array and remove it.
Push one or more values onto the end of an array.
Get a random value or multiple values from an array.
Reject elements that match a given condition.
Reverse the order of elements in an array.
Rotate an array left or right.
Search for a value in an array and return the corresponding key.
Set a value within an array using dot notation.
Remove and return the first element of an array.
Shuffle the array.
Sort an array using a callback function.
Sort an associative array by its keys.
Sort an array by a specific key.
Sort an array by its keys.
Sort an array by its values.
Swap two keys in an array.
Convert an array to a JSON string.
Convert an array to an object.
Remove duplicate values from an array.
Remove duplicate items from an array based on a key or callback.
Remove multiple keys from an array.
Unwrap an array if it contains only one item.
Return all values from an array, resetting numeric keys.
Recursively applies a callback function to each element in an array.
Select a random element based on weighted probabilities.
Wrap a value in an array.
Filter an array using a callback.
Compute the exclusive difference between two arrays.
Details
at line 18
static array
add(array $array, string|int $key, mixed $value)
Add a value to an array if the key does not exist.
at line 32
static array
arrayDivide(array $array)
Split an array into two separate arrays: one with keys, one with values.
at line 44
static array
arrayPluckMulti(array $array, array|string $keys)
Pluck a nested value from an array.
at line 64
static array
arrayShuffleAssoc(array $array)
Shuffle an associative array while preserving keys.
at line 77
static array
collapse(array $array)
Collapse a multi-dimensional array into a single-level array.
at line 97
static array
chunk(array $array, int $size, bool $preserveKeys = false)
Split an array into chunks of a given size.
at line 109
static array
chunkBy(array $array, callable $callback)
Chunk an array into groups based on a callback function.
at line 136
static bool
contains(array $array, mixed $value, bool $strict = false)
Determine if a given value exists in an array.
at line 147
static array
crossJoin(array ...$arrays)
Compute the Cartesian product of multiple arrays.
at line 171
static array
deepMerge(array ...$arrays)
Recursively merge two or more arrays.
at line 194
static array
diffAssocRecursive(array $array1, array $array2)
Recursively compute the difference between two arrays with keys.
at line 216
static array
dot(array $array, string $prepend = '')
Convert a multi-dimensional array into dot notation keys.
at line 238
static array
except(array $array, array $keys)
Get all items except the specified keys.
at line 249
static bool
exists(array $array, string|int $key)
Check if a key exists in an array (non-dot notation).
at line 261
static array
fill(int $startIndex, int $count, mixed $value)
Fill an array with a specified value.
at line 273
static array
filter(array $array, callable $callback)
Filter an array using a callback function.
at line 285
static array
filterByKeys(array $array, array $keys)
Filter an array to include only the specified keys.
at line 296
static array
filterByValue(array $array, callable $callback)
Filter an array by its values.
at line 308
static mixed
first(array $array, callable|null $callback = null, mixed $default = null)
Get the first element that passes a given test.
at line 329
static array
flatten(array $array, int $depth = INF)
Flatten a multi-dimensional array into a single level.
at line 350
static array
flattenWithDepth(array $array, int $depth = INF)
Flatten an array up to a specified depth.
at line 371
static array
flattenKeys(array $array, string $prefix = '')
Convert a multi-dimensional array into dot notation keys.
at line 395
static void
forget(array $array, string|array $keys)
Remove a value from an array using dot notation.
at line 424
static mixed
get(array $array, string $key, mixed $default = null)
Get a value from an array using dot notation.
at line 446
static array
groupBy(array $array, string $key)
Group an array by a given key.
at line 467
static bool
has(array $array, string $key)
Check if an array has a given key using dot notation.
at line 489
static bool
hasAllKeys(array $array, array $keys)
Check if all given keys exist in the array.
at line 500
static bool
hasAnyKey(array $array, array $keys)
Check if at least one key exists in the array.
at line 511
static bool
hasAny(array $array, array $keys)
Determine if at least one of the given keys exists in the array.
at line 525
static array
insertBefore(array $array, string|int $key, string|int $newKey, mixed $value)
Insert an element before a given key in an array.
at line 547
static array
insertAfter(array $array, string|int $key, string|int $newKey, mixed $value)
Insert an element after a given key in an array.
at line 566
static bool
isArray(mixed $value)
Determine if a given value is an array.
at line 577
static bool
isAssoc(array $array)
Determine if an array is associative (i.e., contains at least one non-numeric key).
at line 588
static bool
isEmpty(array|null $array)
Check if the given array is empty.
at line 598
static bool
isNotEmpty(array|null $array)
Check if the given array is not empty.
at line 608
static array
keys(array $array)
Get all the keys from an array.
at line 620
static array
keyBy(array $array, string|int $key)
Reindex an array using a specified key.
at line 643
static mixed
last(array $array, callable|null $callback = null, mixed $default = null)
Get the last element that passes a given test.
at line 654
static array
map(array $array, callable $callback)
Apply a callback to each item in an array.
at line 665
static array
mapWithKeys(array $array, callable $callback)
Map an array while preserving keys.
at line 687
static array
merge(array ...$arrays)
Merge one or more arrays together.
at line 699
static array
only(array $array, array $keys)
Get only the specified keys from an array.
at line 711
static array
partition(array $array, callable $callback)
Partition an array into two arrays: One where the callback returns true, the other where it returns false.
at line 734
static array
pluck(array $array, string $value, string|null $key = null)
Pluck a single key from an array.
at line 759
static array
prepend(array $array, mixed $value, string|int|null $key = null)
Prepend a value to an array.
at line 776
static mixed
pull(array $array, string $key, mixed $default = null)
Retrieve a value from the array and remove it.
at line 789
static array
push(array $array, mixed ...$values)
Push one or more values onto the end of an array.
at line 802
static mixed
random(array $array, int|null $number = null)
Get a random value or multiple values from an array.
at line 823
static array
reject(array $array, callable $callback)
Reject elements that match a given condition.
at line 835
static array
reverse(array $array, bool $preserveKeys = false)
Reverse the order of elements in an array.
at line 847
static array
rotate(array $array, int $positions)
Rotate an array left or right.
at line 864
static string|int|false
search(array $array, mixed $value, bool $strict = false)
Search for a value in an array and return the corresponding key.
at line 877
static void
set(array $array, string $key, mixed $value)
Set a value within an array using dot notation.
at line 899
static mixed
shift(array $array)
Remove and return the first element of an array.
at line 911
static array
shuffle(array $array, int|null $seed = null)
Shuffle the array.
at line 927
static array
sort(array $array, callable|null $callback = null)
Sort an array using a callback function.
at line 945
static array
sortAssoc(array $array, bool $descending = false)
Sort an associative array by its keys.
at line 959
static array
sortBy(array $array, string $key, bool $descending = false)
Sort an array by a specific key.
at line 971
static array
sortByKeys(array $array)
Sort an array by its keys.
at line 982
static array
sortByValues(array $array)
Sort an array by its values.
at line 995
static array
swapKeys(array $array, string|int $key1, string|int $key2)
Swap two keys in an array.
at line 1012
static string
toJson(array $array, int $options = 0)
Convert an array to a JSON string.
at line 1022
static object
toObject(array $array)
Convert an array to an object.
at line 1032
static array
unique(array $array)
Remove duplicate values from an array.
at line 1044
static array
uniqueBy(array $array, string|callable $key)
Remove duplicate items from an array based on a key or callback.
at line 1063
static array
unsetKeys(array $array, array $keys)
Remove multiple keys from an array.
at line 1076
static mixed
unwrap(array $array)
Unwrap an array if it contains only one item.
at line 1086
static array
values(array $array)
Return all values from an array, resetting numeric keys.
at line 1105
static array
walkRecursive(array $array, callable $callback)
Recursively applies a callback function to each element in an array.
This function modifies each value in the array using the provided callback. The callback receives both the value and the key of each array element.
at line 1121
static mixed
weightedRandom(array $array, array $weights)
Select a random element based on weighted probabilities.
at line 1142
static array
wrap(mixed $value)
Wrap a value in an array.
at line 1153
static array
where(array $array, callable $callback)
Filter an array using a callback.
at line 1164
static array
xorDiff(array $array1, array $array2)
Compute the exclusive difference between two arrays.