class Arrays

Array manipulation utilities.

Methods

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

Returns item from array. If it does not exist, it throws an exception, unless a default value is set.

static mixed
getRef(array $array, string|int|array $key)

Returns reference to array item. If the index does not exist, new one is created with value null.

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

Recursively merges two arrays. Useful for merging tree structures. Behaves like the + operator: key/value pairs from the second array are added to the first, with the first array's values taking precedence on key collisions. Nested arrays are merged recursively instead of replaced.

static int|null
getKeyOffset(array $array, string|int $key)

Returns zero-indexed position of given array key. Returns null if key is not found.

static int|null
searchKey(array $array, string|int $key) deprecated

No description

static bool
contains(array $array, mixed $value)

Tests an array for the presence of value.

static mixed
first(array $array, callable|null $predicate = null, callable|null $else = null)

No description

static mixed
last(array $array, callable|null $predicate = null, callable|null $else = null)

No description

static int|string|null
firstKey(array $array, callable|null $predicate = null)

No description

static int|string|null
lastKey(array $array, callable|null $predicate = null)

No description

static void
insertBefore(array $array, string|int|null $key, array $inserted)

Inserts the contents of the $inserted array into the $array immediately before the $key.

static void
insertAfter(array $array, string|int|null $key, array $inserted)

Inserts the contents of the $inserted array into the $array immediately after the $key.

static bool
renameKey(array $array, string|int $oldKey, string|int $newKey)

Renames key in array.

static array
grep(array $array, string $pattern, bool|int $invert = false)

Returns only those array items, which matches a regular expression $pattern.

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

Transforms multidimensional array to flat array.

static bool
isList(mixed $value)

Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.

static array|stdClass
associate(array $array, string|array $path)

Transforms a flat array of rows into an associative tree using a path expression like 'field|field[]field->field=field'.

static array
normalize(array $array, mixed $filling = null)

Converts array to associative: items with numeric keys are converted to keys, with $filling as their value.

static mixed
pick(array $array, string|int $key, mixed $default = null)

Returns and removes the value of an item from an array. If it does not exist, it throws an exception, or returns $default, if provided.

static bool
some(iterable $array, callable $predicate)

No description

static bool
every(iterable $array, callable $predicate)

No description

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

No description

static array
map(iterable $array, callable $transformer)

No description

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

No description

static array
invoke(iterable $callbacks, mixed ...$args)

Invokes all callbacks and returns array of results.

static array
invokeMethod(iterable $objects, string $method, mixed ...$args)

Invokes method on every object in an array and returns array of results.

static object
toObject(iterable $array, object $object)

Copies the elements of the $array array to the $object object and then returns it.

static int|string
toKey(mixed $value)

Converts value to array key.

static array
wrap(array $array, string $prefix = '', string $suffix = '')

Returns a copy of $array where every item is cast to string and wrapped with $prefix and $suffix.

Details

at line 32
static mixed get(array $array, string|int|array $key, mixed $default = null)

Returns item from array. If it does not exist, it throws an exception, unless a default value is set.

Parameters

array $array
string|int|array $key
mixed $default

Return Value

mixed

Exceptions

InvalidArgumentException

at line 58
static mixed getRef(array $array, string|int|array $key)

Returns reference to array item. If the index does not exist, new one is created with value null.

Parameters

array $array
string|int|array $key

Return Value

mixed

Exceptions

InvalidArgumentException

at line 82
static array mergeTree(array $array1, array $array2)

Recursively merges two arrays. Useful for merging tree structures. Behaves like the + operator: key/value pairs from the second array are added to the first, with the first array's values taking precedence on key collisions. Nested arrays are merged recursively instead of replaced.

Parameters

array $array1
array $array2

Return Value

array

at line 99
static int|null getKeyOffset(array $array, string|int $key)

Returns zero-indexed position of given array key. Returns null if key is not found.

Parameters

array $array
string|int $key

Return Value

int|null

at line 109
static int|null searchKey(array $array, string|int $key) deprecated

deprecated use getKeyOffset()

No description

Parameters

array $array
string|int $key

Return Value

int|null

at line 119
static bool contains(array $array, mixed $value)

Tests an array for the presence of value.

Parameters

array $array
mixed $value

Return Value

bool

at line 135
static mixed first(array $array, callable|null $predicate = null, callable|null $else = null)

No description

Parameters

array $array
callable|null $predicate
callable|null $else

Return Value

mixed

at line 154
static mixed last(array $array, callable|null $predicate = null, callable|null $else = null)

No description

Parameters

array $array
callable|null $predicate
callable|null $else

Return Value

mixed

at line 171
static int|string|null firstKey(array $array, callable|null $predicate = null)

No description

Parameters

array $array
callable|null $predicate

Return Value

int|string|null

at line 193
static int|string|null lastKey(array $array, callable|null $predicate = null)

No description

Parameters

array $array
callable|null $predicate

Return Value

int|string|null

at line 207
static void insertBefore(array $array, string|int|null $key, array $inserted)

Inserts the contents of the $inserted array into the $array immediately before the $key.

If $key is null (or does not exist), it is inserted at the beginning.

Parameters

array $array
string|int|null $key
array $inserted

Return Value

void

at line 222
static void insertAfter(array $array, string|int|null $key, array $inserted)

Inserts the contents of the $inserted array into the $array immediately after the $key.

If $key is null (or does not exist), it is inserted at the end.

Parameters

array $array
string|int|null $key
array $inserted

Return Value

void

at line 238
static bool renameKey(array $array, string|int $oldKey, string|int $newKey)

Renames key in array.

Parameters

array $array
string|int $oldKey
string|int $newKey

Return Value

bool

at line 259
static array grep(array $array, string $pattern, bool|int $invert = false)

Returns only those array items, which matches a regular expression $pattern.

Parameters

array $array
string $pattern
bool|int $invert

Return Value

array

at line 276
static array flatten(array $array, bool $preserveKeys = false)

Transforms multidimensional array to flat array.

Parameters

array $array
bool $preserveKeys

Return Value

array

at line 291
static bool isList(mixed $value)

Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.

Parameters

mixed $value

Return Value

bool

at line 303
static array|stdClass associate(array $array, string|array $path)

Transforms a flat array of rows into an associative tree using a path expression like 'field|field[]field->field=field'.

Parameters

array $array
string|array $path

Return Value

array|stdClass

at line 360
static array normalize(array $array, mixed $filling = null)

Converts array to associative: items with numeric keys are converted to keys, with $filling as their value.

Parameters

array $array
mixed $filling

Return Value

array

at line 380
static mixed pick(array $array, string|int $key, mixed $default = null)

Returns and removes the value of an item from an array. If it does not exist, it throws an exception, or returns $default, if provided.

Parameters

array $array
string|int $key
mixed $default

Return Value

mixed

Exceptions

InvalidArgumentException

at line 403
static bool some(iterable $array, callable $predicate)

No description

Parameters

iterable $array
callable $predicate

Return Value

bool

at line 422
static bool every(iterable $array, callable $predicate)

No description

Parameters

iterable $array
callable $predicate

Return Value

bool

at line 442
static array filter(array $array, callable $predicate)

No description

Parameters

array $array
callable $predicate

Return Value

array

at line 463
static array map(iterable $array, callable $transformer)

No description

Parameters

iterable $array
callable $transformer

Return Value

array

at line 485
static array mapWithKeys(array $array, callable $transformer)

No description

Parameters

array $array
callable $transformer

Return Value

array

at line 504
static array invoke(iterable $callbacks, mixed ...$args)

Invokes all callbacks and returns array of results.

Parameters

iterable $callbacks
mixed ...$args

Return Value

array

at line 520
static array invokeMethod(iterable $objects, string $method, mixed ...$args)

Invokes method on every object in an array and returns array of results.

Parameters

iterable $objects
string $method
mixed ...$args

Return Value

array

at line 538
static object toObject(iterable $array, object $object)

Copies the elements of the $array array to the $object object and then returns it.

Parameters

iterable $array
object $object

Return Value

object

at line 551
static int|string toKey(mixed $value)

Converts value to array key.

Parameters

mixed $value

Return Value

int|string

at line 562
static array wrap(array $array, string $prefix = '', string $suffix = '')

Returns a copy of $array where every item is cast to string and wrapped with $prefix and $suffix.

Parameters

array $array
string $prefix
string $suffix

Return Value

array

Traits

Prevents instantiation.