class ArraySet

Array utility class with functions for both chainable operations.

Properties

protected array $items

Items for array.

protected mixed $lastResult

The last result.

Methods

__construct(array $items = [])

Constructor to initialize the array.

add(string $key, mixed $value)

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

array
all()

Get all items in the array.

asort()

Sort the array while maintaining index association.

arsort()

Sort the array in descending order while maintaining index association.

chunk(int $size)

Split an array into chunks.

clear()

Clear all items in the array.

collapse()

Collapse a multi-dimensional array into a single level.

column(string|int $columnKey)

Extract a single column from a multi-dimensional array.

static ArraySet
combine(array $keys, array $values)

Combine two arrays, one as keys and one as values.

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

Check if an array contains a specific value.

crossJoin(array ...$arrays)

Compute the Cartesian product of multiple arrays.

count()

Get the count of elements in the array.

diff(array $array)

Get the difference between the current array and another array.

dot(string $prepend = '')

Convert an array into dot notation.

each(callable $callback)

Iterate over each item and apply a callback.

except(array|string $keys)

Remove specified keys from the array.

exists(string $key)

Determine if a key exists in an array.

fill(int $start, int $count, mixed $value)

Fill an array with a specified value.

filter(callable $callback)

Filters the array using a callback function.

first(callable|null $callback = null)

Get the first element that matches a condition.

firstKey()

Get the first key of the array.

flatten()

Flatten a multi-dimensional array into a single level.

flip()

Swap keys with values in the array.

forget(string $key)

Remove a specific key from the array.

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

Get a value from the array using dot notation.

has(string|int $key)

Check if a key exists in the array.

hasAny(array|string $keys)

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

intersect(array $array)

Get the intersection of the current array and another array.

intersectKeys(array $array)

Get elements whose keys exist in another array.

implode(string $separator)

Join array elements with a string.

isArray(mixed $value)

Check if the given value is an array.

isEmpty()

Check if the array is empty.

keys()

Get all keys of the array.

keyBy(string $key)

Key an array by a specific field.

krsort()

Sort the array by keys in descending order.

ksort()

Sort the array by keys in ascending order.

last(callable|null $callback = null)

Get the last element that matches a condition.

lastKey()

Get the last key of the array.

static ArraySet
make(mixed $items = [])

Create a new Arr instance from an array or a single value.

map(callable $callback)

Apply a callback function to each item in the array.

mapRecursive(callable $callback)

Apply a recursive callback function to each element in the array.

mapWithKeys(callable $callback)

Map an array using a callback that defines both keys and values.

merge(array $array)

Merge the current array with another array.

multisort(int $sortFlags = SORT_REGULAR)

Sort multiple arrays or multi-dimensional arrays.

only(array|string $keys)

Return only the specified keys from the array.

pad(int $size, mixed $value)

Pad an array to the specified length with a value.

pluck(string $key)

Extract a specific key's values.

prepend(mixed $value)

Prepend a value to the beginning of the array.

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

Remove and return a value from the array.

push(mixed ...$values)

Add one or more values to the array.

random(int|null $number = null)

Get a random value or multiple values from the array.

reduce(callable $callback, mixed $initial = null)

Reduce the array to a single value using a callback.

replace(array $array)

Replace values in the array with values from another array.

mixed
result()

Retrieve the result of the last operation.

reverse()

Reverse the order of the array.

rsort()

Sort the array in descending order.

search(mixed $value)

Search for a value in the array and return its key.

set(string $key, mixed $value)

Set a value in the array using dot notation.

shift()

Remove and return the first item from the array.

shuffle()

Shuffle the elements of the array.

shuffleAssociative()

Shuffle an array while preserving keys.

slice(int $offset, int|null $length = null)

Extract a slice of the array.

sort(int $sortFlags = SORT_REGULAR)

Sort the array in ascending order.

splice(int $offset, int|null $length = null, array $replacement = [])

Remove and replace a portion of the array.

udiff(array $array, callable $callback)

Compute the difference between arrays using a custom comparison function.

unique()

Remove duplicate values from the array.

usort(callable $callback)

Sort the array using a user-defined comparison function.

values()

Get all values of the array.

walk(callable $callback)

Apply a user function to every item in the array.

walkRecursive(callable $callback)

Recursively apply a user function to every item in the array.

wrap(mixed $value)

Wrap a value in an array if it is not already an array.

where(callable $callback)

Filter the array using a callback function.

Details

at line 29
__construct(array $items = [])

Constructor to initialize the array.

Parameters

array $items

The initial array.

at line 42
ArraySet add(string $key, mixed $value)

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

Parameters

string $key

The key to check.

mixed $value

The value to add.

Return Value

ArraySet

at line 55
array all()

Get all items in the array.

Return Value

array

The stored array.

at line 68
ArraySet asort()

Sort the array while maintaining index association.

This method sorts the array in ascending order while preserving the original keys.

Return Value

ArraySet

The modified Arr instance.

at line 82
ArraySet arsort()

Sort the array in descending order while maintaining index association.

This method sorts the array in descending order while preserving the original keys.

Return Value

ArraySet

The modified Arr instance.

at line 94
ArraySet chunk(int $size)

Split an array into chunks.

Parameters

int $size

The size of each chunk.

Return Value

ArraySet

at line 105
ArraySet clear()

Clear all items in the array.

Return Value

ArraySet

at line 116
ArraySet collapse()

Collapse a multi-dimensional array into a single level.

Return Value

ArraySet

at line 132
ArraySet column(string|int $columnKey)

Extract a single column from a multi-dimensional array.

Parameters

string|int $columnKey

The column key.

Return Value

ArraySet

at line 145
static ArraySet combine(array $keys, array $values)

Combine two arrays, one as keys and one as values.

Parameters

array $keys

The keys.

array $values

The values.

Return Value

ArraySet

at line 160
ArraySet contains(mixed $value, bool $strict = false)

Check if an array contains a specific value.

Parameters

mixed $value

The value to search for.

bool $strict

Whether to perform strict comparison.

Return Value

ArraySet

at line 172
ArraySet crossJoin(array ...$arrays)

Compute the Cartesian product of multiple arrays.

Parameters

array ...$arrays

Arrays to join.

Return Value

ArraySet

at line 193
ArraySet count()

Get the count of elements in the array.

Return Value

ArraySet

at line 205
ArraySet diff(array $array)

Get the difference between the current array and another array.

Parameters

array $array

The array to compare.

Return Value

ArraySet

at line 217
ArraySet dot(string $prepend = '')

Convert an array into dot notation.

Parameters

string $prepend

A string to prepend before keys.

Return Value

ArraySet

at line 243
ArraySet each(callable $callback)

Iterate over each item and apply a callback.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 257
ArraySet except(array|string $keys)

Remove specified keys from the array.

Parameters

array|string $keys

The keys to remove.

Return Value

ArraySet

at line 271
ArraySet exists(string $key)

Determine if a key exists in an array.

Parameters

string $key

The key to check.

Return Value

ArraySet

at line 285
ArraySet fill(int $start, int $count, mixed $value)

Fill an array with a specified value.

Parameters

int $start

Index to start filling.

int $count

Number of elements to insert.

mixed $value

The value to insert.

Return Value

ArraySet

at line 297
ArraySet filter(callable $callback)

Filters the array using a callback function.

Parameters

callable $callback

The function to apply for filtering.

Return Value

ArraySet

at line 309
ArraySet first(callable|null $callback = null)

Get the first element that matches a condition.

Parameters

callable|null $callback

A callback function to test elements.

Return Value

ArraySet

at line 329
ArraySet firstKey()

Get the first key of the array.

Return Value

ArraySet

at line 340
ArraySet flatten()

Flatten a multi-dimensional array into a single level.

Return Value

ArraySet

at line 355
ArraySet flip()

Swap keys with values in the array.

Return Value

ArraySet

at line 367
ArraySet forget(string $key)

Remove a specific key from the array.

Parameters

string $key

The key to remove.

Return Value

ArraySet

at line 380
ArraySet get(string $key, mixed $default = null)

Get a value from the array using dot notation.

Parameters

string $key

The key in dot notation.

mixed $default

The default value if the key is not found.

Return Value

ArraySet

at line 403
ArraySet has(string|int $key)

Check if a key exists in the array.

Parameters

string|int $key

The key to check (can be a string or integer).

Return Value

ArraySet

Returns the current ArraySet instance.

at line 415
ArraySet hasAny(array|string $keys)

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

Parameters

array|string $keys

The keys to check.

Return Value

ArraySet

at line 428
ArraySet intersect(array $array)

Get the intersection of the current array and another array.

Parameters

array $array

The array to compare.

Return Value

ArraySet

at line 440
ArraySet intersectKeys(array $array)

Get elements whose keys exist in another array.

Parameters

array $array

The array to compare.

Return Value

ArraySet

at line 452
ArraySet implode(string $separator)

Join array elements with a string.

Parameters

string $separator

The separator string.

Return Value

ArraySet

at line 464
ArraySet isArray(mixed $value)

Check if the given value is an array.

Parameters

mixed $value

The value to check.

Return Value

ArraySet

at line 475
ArraySet isEmpty()

Check if the array is empty.

Return Value

ArraySet

at line 486
ArraySet keys()

Get all keys of the array.

Return Value

ArraySet

at line 498
ArraySet keyBy(string $key)

Key an array by a specific field.

Parameters

string $key

The field to use as keys.

Return Value

ArraySet

at line 509
ArraySet krsort()

Sort the array by keys in descending order.

Return Value

ArraySet

at line 520
ArraySet ksort()

Sort the array by keys in ascending order.

Return Value

ArraySet

at line 532
ArraySet last(callable|null $callback = null)

Get the last element that matches a condition.

Parameters

callable|null $callback

A callback function to test elements.

Return Value

ArraySet

at line 553
ArraySet lastKey()

Get the last key of the array.

Return Value

ArraySet

at line 569
static ArraySet make(mixed $items = [])

Create a new Arr instance from an array or a single value.

This function ensures that the provided input is always treated as an array. If a non-array value (e.g., null, string, int) is passed, it wraps it into an array automatically.

Parameters

mixed $items

The array or single value to wrap.

Return Value

ArraySet

A new Arr instance.

at line 584
ArraySet map(callable $callback)

Apply a callback function to each item in the array.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 596
ArraySet mapRecursive(callable $callback)

Apply a recursive callback function to each element in the array.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 615
ArraySet mapWithKeys(callable $callback)

Map an array using a callback that defines both keys and values.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 636
ArraySet merge(array $array)

Merge the current array with another array.

Parameters

array $array

The array to merge with.

Return Value

ArraySet

at line 648
ArraySet multisort(int $sortFlags = SORT_REGULAR)

Sort multiple arrays or multi-dimensional arrays.

Parameters

int $sortFlags

The sorting flags.

Return Value

ArraySet

at line 660
ArraySet only(array|string $keys)

Return only the specified keys from the array.

Parameters

array|string $keys

The keys to include.

Return Value

ArraySet

at line 673
ArraySet pad(int $size, mixed $value)

Pad an array to the specified length with a value.

Parameters

int $size

The required size of the array.

mixed $value

The value to pad with.

Return Value

ArraySet

at line 685
ArraySet pluck(string $key)

Extract a specific key's values.

Parameters

string $key

The key to pluck.

Return Value

ArraySet

at line 697
ArraySet prepend(mixed $value)

Prepend a value to the beginning of the array.

Parameters

mixed $value

The value to prepend.

Return Value

ArraySet

at line 710
ArraySet pull(string $key, mixed $default = null)

Remove and return a value from the array.

Parameters

string $key

The key to retrieve and remove.

mixed $default

The default value if the key is not found.

Return Value

ArraySet

at line 723
ArraySet push(mixed ...$values)

Add one or more values to the array.

Parameters

mixed ...$values

The values to add.

Return Value

ArraySet

at line 735
ArraySet random(int|null $number = null)

Get a random value or multiple values from the array.

Parameters

int|null $number

Number of elements to retrieve.

Return Value

ArraySet

at line 750
ArraySet reduce(callable $callback, mixed $initial = null)

Reduce the array to a single value using a callback.

Parameters

callable $callback

The callback function.

mixed $initial

The initial value.

Return Value

ArraySet

at line 762
ArraySet replace(array $array)

Replace values in the array with values from another array.

Parameters

array $array

The array with replacement values.

Return Value

ArraySet

at line 773
mixed result()

Retrieve the result of the last operation.

Return Value

mixed

at line 783
ArraySet reverse()

Reverse the order of the array.

Return Value

ArraySet

at line 794
ArraySet rsort()

Sort the array in descending order.

Return Value

ArraySet

Search for a value in the array and return its key.

Parameters

mixed $value

The value to search for.

Return Value

ArraySet

at line 819
ArraySet set(string $key, mixed $value)

Set a value in the array using dot notation.

Parameters

string $key

The key in dot notation.

mixed $value

The value to set.

Return Value

ArraySet

at line 840
ArraySet shift()

Remove and return the first item from the array.

Return Value

ArraySet

at line 851
ArraySet shuffle()

Shuffle the elements of the array.

Return Value

ArraySet

at line 862
ArraySet shuffleAssociative()

Shuffle an array while preserving keys.

Return Value

ArraySet

at line 877
ArraySet slice(int $offset, int|null $length = null)

Extract a slice of the array.

Parameters

int $offset

The index to start the slice.

int|null $length

The number of elements to extract.

Return Value

ArraySet

at line 889
ArraySet sort(int $sortFlags = SORT_REGULAR)

Sort the array in ascending order.

Parameters

int $sortFlags

Flags for sorting (default: SORT_REGULAR).

Return Value

ArraySet

at line 903
ArraySet splice(int $offset, int|null $length = null, array $replacement = [])

Remove and replace a portion of the array.

Parameters

int $offset

The index to start the splice.

int|null $length

The number of elements to remove.

array $replacement

The replacement values.

Return Value

ArraySet

at line 916
ArraySet udiff(array $array, callable $callback)

Compute the difference between arrays using a custom comparison function.

Parameters

array $array

The array to compare.

callable $callback

The comparison function.

Return Value

ArraySet

at line 927
ArraySet unique()

Remove duplicate values from the array.

Return Value

ArraySet

at line 939
ArraySet usort(callable $callback)

Sort the array using a user-defined comparison function.

Parameters

callable $callback

The comparison function.

Return Value

ArraySet

at line 950
ArraySet values()

Get all values of the array.

Return Value

ArraySet

at line 962
ArraySet walk(callable $callback)

Apply a user function to every item in the array.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 974
ArraySet walkRecursive(callable $callback)

Recursively apply a user function to every item in the array.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet

at line 986
ArraySet wrap(mixed $value)

Wrap a value in an array if it is not already an array.

Parameters

mixed $value

The value to wrap.

Return Value

ArraySet

at line 1002
ArraySet where(callable $callback)

Filter the array using a callback function.

Parameters

callable $callback

The function to apply.

Return Value

ArraySet