Arrays
class Arrays
Array tools library.
Methods
Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
Returns reference to array item. If the index does not exist, new one is created with value null.
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.
Returns zero-indexed position of given array key. Returns null if key is not found.
Inserts the contents of the $inserted array into the $array immediately after the $key.
Inserts the contents of the $inserted array into the $array before the $key.
Renames key in array.
Returns only those array items, which matches a regular expression $pattern.
Transforms multidimensional array to flat array.
Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
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.
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
.
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
.
Calls $callback on all elements in the array and returns the array of return values.
Copies the elements of the $array array to the $object object and then returns it.
Converts value to array key.
Details
at line 32
static T
get(array $array, string|int|array-key[] $key, T $default = null)
Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
at line 56
static T
getRef(array $array, string|int|array-key[] $key)
Returns reference to array item. If the index does not exist, new one is created with value null.
at line 79
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.
at line 96
static int|null
searchKey(array $array, array-key $key)
Returns zero-indexed position of given array key. Returns null if key is not found.
at line 107
static void
insertBefore(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 beginning.
at line 121
static void
insertAfter(array $array, string|int|null $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 end.
at line 136
static bool
renameKey(array $array, array-key $oldKey, array-key $newKey)
Renames key in array.
at line 156
static array
grep(array $array, string $pattern, int $flags = 0)
Returns only those array items, which matches a regular expression $pattern.
at line 165
static array
flatten(array $array, bool $preserveKeys = false)
Transforms multidimensional array to flat array.
at line 180
static bool
isList(mixed $value)
Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
at line 191
static array|stdClass
associate(array $array, string|string[] $path)
Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
at line 246
static array
normalize(array $array, mixed $filling = null)
Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
at line 266
static T
pick(array $array, array-key $key, T $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.
at line 286
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
.
at line 301
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
.
at line 316
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
.
at line 332
static T
toObject(array $array, T $object)
Copies the elements of the $array array to the $object object and then returns it.
at line 346
static array-key
toKey(mixed $value)
Converts value to array key.
Traits
Static class.