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.
Tests an array for the presence of value.
Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
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.
No description
No description
No description
No description
Invokes all callbacks and returns array of results.
Invokes method on every object in an array and returns array of results.
Copies the elements of the $array array to the $object object and then returns it.
Converts value to array key.
Returns copy of the $array where every item is converted to string and prefixed by $prefix and suffixed by $suffix.
Details
at line 33
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.
at line 59
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.
at line 83
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 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.
at line 108
static int|null
searchKey(array $array, $key)
deprecated
deprecated
No description
at line 117
static bool
contains(array $array, mixed $value)
Tests an array for the presence of value.
at line 130
static mixed
first(array $array, callable|null $predicate = null, callable|null $else = null)
Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
The $predicate has the signature function (mixed $value, int|string $key, array $array): bool
.
at line 146
static mixed
last(array $array, callable|null $predicate = null, callable|null $else = null)
Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
The $predicate has the signature function (mixed $value, int|string $key, array $array): bool
.
at line 159
static int|string|null
firstKey(array $array, callable|null $predicate = null)
Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
The $predicate has the signature function (mixed $value, int|string $key, array $array): bool
.
at line 177
static int|string|null
lastKey(array $array, callable|null $predicate = null)
Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
The $predicate has the signature function (mixed $value, int|string $key, array $array): bool
.
at line 189
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 202
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 217
static bool
renameKey(array $array, string|int $oldKey, string|int $newKey)
Renames key in array.
at line 238
static array
grep(array $array, string $pattern, bool|int $invert = false)
Returns only those array items, which matches a regular expression $pattern.
at line 253
static array
flatten(array $array, bool $preserveKeys = false)
Transforms multidimensional array to flat array.
at line 268
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 281
static array|stdClass
associate(array $array, string|string[] $path)
Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
at line 334
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 354
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.
at line 378
static bool
some(iterable $array, callable $predicate)
No description
at line 398
static bool
every(iterable $array, callable $predicate)
No description
at line 419
static array
filter(array $array, callable $predicate)
No description
at line 441
static array
map(iterable $array, callable $transformer)
No description
at line 456
static array
invoke(iterable $callbacks, ...$args)
Invokes all callbacks and returns array of results.
at line 471
static array
invokeMethod(iterable $objects, string $method, ...$args)
Invokes method on every object in an array and returns array of results.
at line 488
static object
toObject(iterable $array, object $object)
Copies the elements of the $array array to the $object object and then returns it.
at line 501
static int|string
toKey(mixed $value)
Converts value to array key.
at line 513
static array
wrap(array $array, string $prefix = '', string $suffix = '')
Returns copy of the $array where every item is converted to string and prefixed by $prefix and suffixed by $suffix.
Traits
Static class.