class Arrays

Array tools library.

Traits

Static class.

Methods

static mixed
get(array $array, $key, $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, $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 fields. It is useful, for example, for merging tree structures. It behaves as the + operator for array, ie. it adds a key/value pair from the second array to the first one and retains the value from the first array in the case of a key collision.

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

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

static void
insertBefore(array $array, $key, array $inserted)

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

static void
insertAfter(array $array, $key, array $inserted)

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

static bool
renameKey(array $array, $oldKey, $newKey)

Renames key in array.

static array
grep(array $array, string $pattern, int $flags = 0)

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, $path)

Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.

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

Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.

static mixed
pick(array $array, $key, $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(array $array, callable $callback)

Tests whether at least one element in the array passes the test implemented by the provided callback with signature function ($value, $key, array $array): bool.

static bool
every(array $array, callable $callback)

Tests whether all elements in the array pass the test implemented by the provided function, which has the signature function ($value, $key, array $array): bool.

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

Calls $callback on all elements in the array and returns the array of return values.

static object
toObject(array $array, $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.

Details

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

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

Parameters

array $array
$key
$default

Return Value

mixed

Exceptions

InvalidArgumentException if item does not exist and default value is not provided

at line 52
static mixed getRef(array $array, $key)

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

Parameters

array $array
$key

Return Value

mixed

Exceptions

InvalidArgumentException if traversed item is not an array

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

Recursively merges two fields. It is useful, for example, for merging tree structures. It behaves as the + operator for array, ie. it adds a key/value pair from the second array to the first one and retains the value from the first array in the case of a key collision.

Parameters

array $array1
array $array2

Return Value

array

at line 87
static int|null searchKey(array $array, $key)

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

Parameters

array $array
$key

Return Value

int|null offset if it is found, null otherwise

at line 98
static void insertBefore(array $array, $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
$key
array $inserted

Return Value

void

at line 112
static void insertAfter(array $array, $key, array $inserted)

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

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

Parameters

array $array
$key
array $inserted

Return Value

void

at line 127
static bool renameKey(array $array, $oldKey, $newKey)

Renames key in array.

Parameters

array $array
$oldKey
$newKey

Return Value

bool

at line 146
static array grep(array $array, string $pattern, int $flags = 0)

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

Parameters

array $array
string $pattern
int $flags

Return Value

array

Exceptions

RegexpException on compilation or runtime error

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

Transforms multidimensional array to flat array.

Parameters

array $array
bool $preserveKeys

Return Value

array

at line 170
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 181
static array|stdClass associate(array $array, $path)

Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.

Parameters

array $array
$path

Return Value

array|stdClass

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

Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.

Parameters

array $array
$filling

Return Value

array

at line 254
static mixed pick(array $array, $key, $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
$key
$default

Return Value

mixed

Exceptions

InvalidArgumentException if item does not exist and default value is not provided

at line 274
static bool some(array $array, callable $callback)

Tests whether at least one element in the array passes the test implemented by the provided callback with signature function ($value, $key, array $array): bool.

Parameters

array $array
callable $callback

Return Value

bool

at line 289
static bool every(array $array, callable $callback)

Tests whether all elements in the array pass the test implemented by the provided function, which has the signature function ($value, $key, array $array): bool.

Parameters

array $array
callable $callback

Return Value

bool

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

Calls $callback on all elements in the array and returns the array of return values.

The callback has the signature function ($value, $key, array $array): bool.

Parameters

array $array
callable $callback

Return Value

array

at line 319
static object toObject(array $array, $object)

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

Parameters

array $array
$object

Return Value

object

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

Converts value to array key.

Parameters

mixed $value

Return Value

int|string