class Arr

Contains functions that support array operations.

Methods

static array
add(array $array, string|int $key, mixed $value)

Add a value to an array if the key does not exist.

static array
arrayDivide(array $array)

Split an array into two separate arrays: one with keys, one with values.

static array
arrayPluckMulti(array $array, array|string $keys)

Pluck a nested value from an array.

static array
arrayShuffleAssoc(array $array)

Shuffle an associative array while preserving keys.

static array
collapse(array $array)

Collapse a multi-dimensional array into a single-level array.

static array
chunk(array $array, int $size, bool $preserveKeys = false)

Split an array into chunks of a given size.

static array
chunkBy(array $array, callable $callback)

Chunk an array into groups based on a callback function.

static bool
contains(array $array, mixed $value, bool $strict = false)

Determine if a given value exists in an array.

static array
crossJoin(array ...$arrays)

Compute the Cartesian product of multiple arrays.

static array
deepMerge(array ...$arrays)

Recursively merge two or more arrays.

static array
diffAssocRecursive(array $array1, array $array2)

Recursively compute the difference between two arrays with keys.

static array
dot(array $array, string $prepend = '')

Convert a multi-dimensional array into dot notation keys.

static array
except(array $array, array $keys)

Get all items except the specified keys.

static bool
exists(array $array, string|int $key)

Check if a key exists in an array (non-dot notation).

static array
fill(int $startIndex, int $count, mixed $value)

Fill an array with a specified value.

static array
filter(array $array, callable $callback)

Filter an array using a callback function.

static array
filterByKeys(array $array, array $keys)

Filter an array to include only the specified keys.

static array
filterByValue(array $array, callable $callback)

Filter an array by its values.

static mixed
first(array $array, callable|null $callback = null, mixed $default = null)

Get the first element that passes a given test.

static array
flatten(array $array, int $depth = INF)

Flatten a multi-dimensional array into a single level.

static array
flattenWithDepth(array $array, int $depth = INF)

Flatten an array up to a specified depth.

static array
flattenKeys(array $array, string $prefix = '')

Convert a multi-dimensional array into dot notation keys.

static void
forget(array $array, string|array $keys)

Remove a value from an array using dot notation.

static mixed
get(array $array, string $key, mixed $default = null)

Get a value from an array using dot notation.

static array
groupBy(array $array, string $key)

Group an array by a given key.

static bool
has(array $array, string $key)

Check if an array has a given key using dot notation.

static bool
hasAllKeys(array $array, array $keys)

Check if all given keys exist in the array.

static bool
hasAnyKey(array $array, array $keys)

Check if at least one key exists in the array.

static bool
hasAny(array $array, array $keys)

Determine if at least one of the given keys exists in the array.

static array
insertBefore(array $array, string|int $key, string|int $newKey, mixed $value)

Insert an element before a given key in an array.

static array
insertAfter(array $array, string|int $key, string|int $newKey, mixed $value)

Insert an element after a given key in an array.

static bool
isArray(mixed $value)

Determine if a given value is an array.

static bool
isAssoc(array $array)

Determine if an array is associative (i.e., contains at least one non-numeric key).

static bool
isEmpty(array|null $array)

Check if the given array is empty.

static bool
isNotEmpty(array|null $array)

Check if the given array is not empty.

static array
keys(array $array)

Get all the keys from an array.

static array
keyBy(array $array, string|int $key)

Reindex an array using a specified key.

static mixed
last(array $array, callable|null $callback = null, mixed $default = null)

Get the last element that passes a given test.

static array
map(array $array, callable $callback)

Apply a callback to each item in an array.

static array
mapWithKeys(array $array, callable $callback)

Map an array while preserving keys.

static array
merge(array ...$arrays)

Merge one or more arrays together.

static array
only(array $array, array $keys)

Get only the specified keys from an array.

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.

static array
pluck(array $array, string $value, string|null $key = null)

Pluck a single key from an array.

static array
prepend(array $array, mixed $value, string|int|null $key = null)

Prepend a value to an array.

static mixed
pull(array $array, string $key, mixed $default = null)

Retrieve a value from the array and remove it.

static array
push(array $array, mixed ...$values)

Push one or more values onto the end of an array.

static mixed
random(array $array, int|null $number = null)

Get a random value or multiple values from an array.

static array
reject(array $array, callable $callback)

Reject elements that match a given condition.

static array
reverse(array $array, bool $preserveKeys = false)

Reverse the order of elements in an array.

static array
rotate(array $array, int $positions)

Rotate an array left or right.

static string|int|false
search(array $array, mixed $value, bool $strict = false)

Search for a value in an array and return the corresponding key.

static void
set(array $array, string $key, mixed $value)

Set a value within an array using dot notation.

static mixed
shift(array $array)

Remove and return the first element of an array.

static array
shuffle(array $array, int|null $seed = null)

Shuffle the array.

static array
sort(array $array, callable|null $callback = null)

Sort an array using a callback function.

static array
sortAssoc(array $array, bool $descending = false)

Sort an associative array by its keys.

static array
sortBy(array $array, string $key, bool $descending = false)

Sort an array by a specific key.

static array
sortByKeys(array $array)

Sort an array by its keys.

static array
sortByValues(array $array)

Sort an array by its values.

static array
swapKeys(array $array, string|int $key1, string|int $key2)

Swap two keys in an array.

static string
toJson(array $array, int $options = 0)

Convert an array to a JSON string.

static object
toObject(array $array)

Convert an array to an object.

static array
unique(array $array)

Remove duplicate values from an array.

static array
uniqueBy(array $array, string|callable $key)

Remove duplicate items from an array based on a key or callback.

static array
unsetKeys(array $array, array $keys)

Remove multiple keys from an array.

static mixed
unwrap(array $array)

Unwrap an array if it contains only one item.

static array
values(array $array)

Return all values from an array, resetting numeric keys.

static array
walkRecursive(array $array, callable $callback)

Recursively applies a callback function to each element in an array.

static mixed
weightedRandom(array $array, array $weights)

Select a random element based on weighted probabilities.

static array
wrap(mixed $value)

Wrap a value in an array.

static array
where(array $array, callable $callback)

Filter an array using a callback.

static array
xorDiff(array $array1, array $array2)

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.

Parameters

array $array

The array to modify.

string|int $key

The key to check.

mixed $value

The value to add.

Return Value

array

The modified array.

at line 32
static array arrayDivide(array $array)

Split an array into two separate arrays: one with keys, one with values.

Parameters

array $array

The array to divide.

Return Value

array

An array containing two arrays: [keys, values].

at line 44
static array arrayPluckMulti(array $array, array|string $keys)

Pluck a nested value from an array.

Parameters

array $array

The source array.

array|string $keys

The nested keys to extract.

Return Value

array

The plucked values.

at line 64
static array arrayShuffleAssoc(array $array)

Shuffle an associative array while preserving keys.

Parameters

array $array

The array to shuffle.

Return Value

array

The shuffled array.

at line 77
static array collapse(array $array)

Collapse a multi-dimensional array into a single-level array.

Parameters

array $array

The multi-dimensional array.

Return Value

array

The collapsed array.

at line 97
static array chunk(array $array, int $size, bool $preserveKeys = false)

Split an array into chunks of a given size.

Parameters

array $array

The array to split.

int $size

The size of each chunk.

bool $preserveKeys

Whether to preserve keys.

Return Value

array

An array of chunked arrays.

at line 109
static array chunkBy(array $array, callable $callback)

Chunk an array into groups based on a callback function.

Parameters

array $array

The array to chunk.

callable $callback

The function to determine chunks.

Return Value

array

The chunked array.

at line 136
static bool contains(array $array, mixed $value, bool $strict = false)

Determine if a given value exists in an array.

Parameters

array $array

The array to search.

mixed $value

The value to find.

bool $strict

Whether to perform a strict comparison.

Return Value

bool

True if the value exists, false otherwise.

at line 147
static array crossJoin(array ...$arrays)

Compute the Cartesian product of multiple arrays.

Parameters

array ...$arrays

The arrays to compute the product for.

Return Value

array

The Cartesian product.

at line 171
static array deepMerge(array ...$arrays)

Recursively merge two or more arrays.

Parameters

array ...$arrays

The arrays to merge.

Return Value

array

The merged array.

at line 194
static array diffAssocRecursive(array $array1, array $array2)

Recursively compute the difference between two arrays with keys.

Parameters

array $array1

The first array.

array $array2

The second array.

Return Value

array

The difference.

at line 216
static array dot(array $array, string $prepend = '')

Convert a multi-dimensional array into dot notation keys.

Parameters

array $array

The multi-dimensional array.

string $prepend

The prefix for keys.

Return Value

array

The array with dot notation keys.

at line 238
static array except(array $array, array $keys)

Get all items except the specified keys.

Parameters

array $array

The source array.

array $keys

The keys to exclude.

Return Value

array

The filtered array.

at line 249
static bool exists(array $array, string|int $key)

Check if a key exists in an array (non-dot notation).

Parameters

array $array

The source array.

string|int $key

The key to check.

Return Value

bool

True if the key exists, false otherwise.

at line 261
static array fill(int $startIndex, int $count, mixed $value)

Fill an array with a specified value.

Parameters

int $startIndex

The first index to use.

int $count

The number of elements to insert.

mixed $value

The value to use for filling.

Return Value

array

The filled array.

at line 273
static array filter(array $array, callable $callback)

Filter an array using a callback function.

Parameters

array $array

The source array.

callable $callback

The filtering function.

Return Value

array

The filtered array.

at line 285
static array filterByKeys(array $array, array $keys)

Filter an array to include only the specified keys.

Parameters

array $array

The source array.

array $keys

The keys to keep.

Return Value

array

The filtered array.

at line 296
static array filterByValue(array $array, callable $callback)

Filter an array by its values.

Parameters

array $array

The array to filter.

callable $callback

The function to apply for filtering.

Return Value

array

The filtered array.

at line 308
static mixed first(array $array, callable|null $callback = null, mixed $default = null)

Get the first element that passes a given test.

Parameters

array $array

The source array.

callable|null $callback

The function to determine a match.

mixed $default

The default value if no match is found.

Return Value

mixed

The first matching value or default.

at line 329
static array flatten(array $array, int $depth = INF)

Flatten a multi-dimensional array into a single level.

Parameters

array $array

The multi-dimensional array.

int $depth

The depth limit.

Return Value

array

The flattened array.

at line 350
static array flattenWithDepth(array $array, int $depth = INF)

Flatten an array up to a specified depth.

Parameters

array $array

The multi-dimensional array.

int $depth

The depth limit (default: infinite).

Return Value

array

The flattened array.

at line 371
static array flattenKeys(array $array, string $prefix = '')

Convert a multi-dimensional array into dot notation keys.

Parameters

array $array

The multi-dimensional array.

string $prefix

The prefix for keys.

Return Value

array

The array with flattened keys.

at line 395
static void forget(array $array, string|array $keys)

Remove a value from an array using dot notation.

Parameters

array $array

The source array (passed by reference).

string|array $keys

The key(s) to remove.

Return Value

void

at line 424
static mixed get(array $array, string $key, mixed $default = null)

Get a value from an array using dot notation.

Parameters

array $array

The source array.

string $key

The key using dot notation.

mixed $default

The default value if the key is not found.

Return Value

mixed

The value from the array or the default.

at line 446
static array groupBy(array $array, string $key)

Group an array by a given key.

Parameters

array $array

The array to group.

string $key

The key to group by.

Return Value

array

The grouped array.

at line 467
static bool has(array $array, string $key)

Check if an array has a given key using dot notation.

Parameters

array $array

The source array.

string $key

The key using dot notation.

Return Value

bool

True if the key exists, false otherwise.

at line 489
static bool hasAllKeys(array $array, array $keys)

Check if all given keys exist in the array.

Parameters

array $array

The array to check.

array $keys

The keys to search for.

Return Value

bool

True if all keys exist, otherwise false.

at line 500
static bool hasAnyKey(array $array, array $keys)

Check if at least one key exists in the array.

Parameters

array $array

The array to check.

array $keys

The keys to search for.

Return Value

bool

True if any key exists, otherwise false.

at line 511
static bool hasAny(array $array, array $keys)

Determine if at least one of the given keys exists in the array.

Parameters

array $array

The array to check.

array $keys

The list of keys to check.

Return Value

bool

True if any key exists, false otherwise.

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.

Parameters

array $array

The original array.

string|int $key

The key to insert before.

string|int $newKey

The new key.

mixed $value

The value to insert.

Return Value

array

The modified 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.

Parameters

array $array

The original array.

string|int $key

The key to insert after.

string|int $newKey

The new key.

mixed $value

The value to insert.

Return Value

array

The modified array.

at line 566
static bool isArray(mixed $value)

Determine if a given value is an array.

Parameters

mixed $value

The value to check.

Return Value

bool

True if the value is an array, false otherwise.

at line 577
static bool isAssoc(array $array)

Determine if an array is associative (i.e., contains at least one non-numeric key).

Parameters

array $array

The array to check.

Return Value

bool

True if associative, false otherwise.

at line 588
static bool isEmpty(array|null $array)

Check if the given array is empty.

Parameters

array|null $array

The array to check.

Return Value

bool

True if empty or null, otherwise false.

at line 598
static bool isNotEmpty(array|null $array)

Check if the given array is not empty.

Parameters

array|null $array

The array to check.

Return Value

bool

True if not empty, otherwise false.

at line 608
static array keys(array $array)

Get all the keys from an array.

Parameters

array $array

The array to extract keys from.

Return Value

array

The array of keys.

at line 620
static array keyBy(array $array, string|int $key)

Reindex an array using a specified key.

Parameters

array $array

The source array.

string|int $key

The key to index by.

Return Value

array

The reindexed array.

at line 643
static mixed last(array $array, callable|null $callback = null, mixed $default = null)

Get the last element that passes a given test.

Parameters

array $array

The source array.

callable|null $callback

The function to determine a match.

mixed $default

The default value if no match is found.

Return Value

mixed

The last matching value or default.

at line 654
static array map(array $array, callable $callback)

Apply a callback to each item in an array.

Parameters

array $array

The source array.

callable $callback

The function to apply.

Return Value

array

The modified array.

at line 665
static array mapWithKeys(array $array, callable $callback)

Map an array while preserving keys.

Parameters

array $array

The source array.

callable $callback

The function to apply.

Return Value

array

The modified array with new keys.

at line 687
static array merge(array ...$arrays)

Merge one or more arrays together.

Parameters

array ...$arrays

Arrays to merge.

Return Value

array

The merged array.

at line 699
static array only(array $array, array $keys)

Get only the specified keys from an array.

Parameters

array $array

The source array.

array $keys

The keys to retrieve.

Return Value

array

The filtered 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.

Parameters

array $array

The array to partition.

callable $callback

The callback function.

Return Value

array

An array with two arrays (true, false).

at line 734
static array pluck(array $array, string $value, string|null $key = null)

Pluck a single key from an array.

Parameters

array $array

The source array.

string $value

The key to extract values for.

string|null $key

Optional key to use as array index.

Return Value

array

The plucked values.

at line 759
static array prepend(array $array, mixed $value, string|int|null $key = null)

Prepend a value to an array.

Parameters

array $array

The array to modify.

mixed $value

The value to prepend.

string|int|null $key

Optional key for the prepended value.

Return Value

array

The modified array.

at line 776
static mixed pull(array $array, string $key, mixed $default = null)

Retrieve a value from the array and remove it.

Parameters

array $array

The source array (passed by reference).

string $key

The key using dot notation.

mixed $default

The default value if the key is not found.

Return Value

mixed

The retrieved value or default.

at line 789
static array push(array $array, mixed ...$values)

Push one or more values onto the end of an array.

Parameters

array $array

The array to modify.

mixed ...$values

The values to push.

Return Value

array

The modified array.

at line 802
static mixed random(array $array, int|null $number = null)

Get a random value or multiple values from an array.

Parameters

array $array

The source array.

int|null $number

Number of elements to retrieve.

Return Value

mixed

The random value(s).

at line 823
static array reject(array $array, callable $callback)

Reject elements that match a given condition.

Parameters

array $array

The source array.

callable $callback

The function to determine rejection.

Return Value

array

The modified array.

at line 835
static array reverse(array $array, bool $preserveKeys = false)

Reverse the order of elements in an array.

Parameters

array $array

The array to reverse.

bool $preserveKeys

Whether to preserve keys in the reversed array.

Return Value

array

The reversed array.

at line 847
static array rotate(array $array, int $positions)

Rotate an array left or right.

Parameters

array $array

The array to rotate.

int $positions

Number of positions to rotate (positive for right, negative for left).

Return Value

array

The rotated array.

Search for a value in an array and return the corresponding key.

Parameters

array $array

The array to search in.

mixed $value

The value to search for.

bool $strict

Whether to perform a strict type comparison.

Return Value

string|int|false

The key if found, false otherwise.

at line 877
static void set(array $array, string $key, mixed $value)

Set a value within an array using dot notation.

Parameters

array $array

The source array (passed by reference).

string $key

The key using dot notation.

mixed $value

The value to set.

Return Value

void

at line 899
static mixed shift(array $array)

Remove and return the first element of an array.

Parameters

array $array

The array to shift from (passed by reference).

Return Value

mixed

The removed element or null if the array is empty.

at line 911
static array shuffle(array $array, int|null $seed = null)

Shuffle the array.

Parameters

array $array

The source array.

int|null $seed

Optional seed for deterministic results.

Return Value

array

The shuffled array.

at line 927
static array sort(array $array, callable|null $callback = null)

Sort an array using a callback function.

Parameters

array $array

The array to sort.

callable|null $callback

The comparison function.

Return Value

array

The sorted array.

at line 945
static array sortAssoc(array $array, bool $descending = false)

Sort an associative array by its keys.

Parameters

array $array

The array to sort.

bool $descending

Whether to sort in descending order.

Return Value

array

The sorted array.

at line 959
static array sortBy(array $array, string $key, bool $descending = false)

Sort an array by a specific key.

Parameters

array $array

The array to sort.

string $key

The key to sort by.

bool $descending

Whether to sort in descending order.

Return Value

array

The sorted array.

at line 971
static array sortByKeys(array $array)

Sort an array by its keys.

Parameters

array $array

The array to sort.

Return Value

array

The sorted array.

at line 982
static array sortByValues(array $array)

Sort an array by its values.

Parameters

array $array

The array to sort.

Return Value

array

The sorted array.

at line 995
static array swapKeys(array $array, string|int $key1, string|int $key2)

Swap two keys in an array.

Parameters

array $array

The array to modify.

string|int $key1

The first key.

string|int $key2

The second key.

Return Value

array

The modified array.

at line 1012
static string toJson(array $array, int $options = 0)

Convert an array to a JSON string.

Parameters

array $array

The array to convert.

int $options

JSON encoding options.

Return Value

string

The JSON string.

at line 1022
static object toObject(array $array)

Convert an array to an object.

Parameters

array $array

The array to convert.

Return Value

object

The converted object.

at line 1032
static array unique(array $array)

Remove duplicate values from an array.

Parameters

array $array

The source array.

Return Value

array

The array without duplicate values.

at line 1044
static array uniqueBy(array $array, string|callable $key)

Remove duplicate items from an array based on a key or callback.

Parameters

array $array

The array to filter.

string|callable $key

The key or function to determine uniqueness.

Return Value

array

The unique array.

at line 1063
static array unsetKeys(array $array, array $keys)

Remove multiple keys from an array.

Parameters

array $array

The array to modify.

array $keys

The keys to remove.

Return Value

array

The array without the specified keys.

at line 1076
static mixed unwrap(array $array)

Unwrap an array if it contains only one item.

Parameters

array $array

The array to unwrap.

Return Value

mixed

The single value or the original array.

at line 1086
static array values(array $array)

Return all values from an array, resetting numeric keys.

Parameters

array $array

The input array.

Return Value

array

The array with numeric indexes.

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.

Parameters

array $array

The array to be processed.

callable $callback

The callback function to apply. The callback should accept two parameters:

  • mixed $value (the array value)
  • string|int $key (the array key)

Return Value

array

The modified array with the callback applied to each value.

at line 1121
static mixed weightedRandom(array $array, array $weights)

Select a random element based on weighted probabilities.

Parameters

array $array

The array with weights.

array $weights

The corresponding weights.

Return Value

mixed

A randomly selected item.

at line 1142
static array wrap(mixed $value)

Wrap a value in an array.

Parameters

mixed $value

The value to wrap.

Return Value

array

The wrapped array.

at line 1153
static array where(array $array, callable $callback)

Filter an array using a callback.

Parameters

array $array

The source array.

callable $callback

The function to apply to each element.

Return Value

array

The filtered array.

at line 1164
static array xorDiff(array $array1, array $array2)

Compute the exclusive difference between two arrays.

Parameters

array $array1

The first array.

array $array2

The second array.

Return Value

array

The values that exist only in one of the arrays.