Arrays
in package
uses
StaticClass
Array tools library.
Table of Contents
Methods
- __callStatic() : mixed
- Call to undefined static method.
- associate() : array<string|int, mixed>|stdClass
- Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
- contains() : bool
- Tests an array for the presence of value.
- every() : bool
- Tests whether all elements in the array pass the test implemented by the provided function.
- filter() : array<string|int, mixed>
- Returns a new array containing all key-value pairs matching the given $predicate.
- first() : V|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.
- firstKey() : K|null
- Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
- flatten() : array<string|int, mixed>
- Transforms multidimensional array to flat array.
- get() : T|null
- Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
- getKeyOffset() : int|null
- Returns zero-indexed position of given array key. Returns null if key is not found.
- getRef() : T|null
- Returns reference to array item. If the index does not exist, new one is created with value null.
- grep() : array<string|int, string>
- Returns only those array items, which matches a regular expression $pattern.
- insertAfter() : void
- Inserts the contents of the $inserted array into the $array before the $key.
- insertBefore() : void
- Inserts the contents of the $inserted array into the $array immediately after the $key.
- invoke() : array<string|int, mixed>
- Invokes all callbacks and returns array of results.
- invokeMethod() : array<string|int, mixed>
- Invokes method on every object in an array and returns array of results.
- isList() : ($value is list ? true : false)
- Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
- last() : V|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.
- lastKey() : K|null
- Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
- map() : array<string|int, mixed>
- Returns an array containing the original keys and results of applying the given transform function to each element.
- mapWithKeys() : array<string|int, mixed>
- Returns an array containing new keys and values generated by applying the given transform function to each element.
- mergeTree() : array<string|int, T1|T2>
- 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.
- normalize() : array<string|int, mixed>
- Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
- pick() : T|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.
- renameKey() : bool
- Renames key in array.
- searchKey() : int|null
- some() : bool
- Tests whether at least one element in the array passes the test implemented by the provided function.
- toKey() : int|string
- Converts value to array key.
- toObject() : T
- Copies the elements of the $array array to the $object object and then returns it.
- wrap() : array<string|int, string>
- Returns copy of the $array where every item is converted to string and prefixed by $prefix and suffixed by $suffix.
- __construct() : mixed
- Class is static and cannot be instantiated.
Methods
__callStatic()
Call to undefined static method.
public
static __callStatic(string $name, array<string|int, mixed> $args) : mixed
Parameters
- $name : string
- $args : array<string|int, mixed>
Tags
associate()
Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
public
static associate(array<string|int, mixed> $array, string|array<string|int, string> $path) : array<string|int, mixed>|stdClass
Parameters
- $array : array<string|int, mixed>
- $path : string|array<string|int, string>
Return values
array<string|int, mixed>|stdClasscontains()
Tests an array for the presence of value.
public
static contains(array<string|int, mixed> $array, mixed $value) : bool
Parameters
- $array : array<string|int, mixed>
- $value : mixed
Return values
boolevery()
Tests whether all elements in the array pass the test implemented by the provided function.
public
static every(iterable<string|int, mixed> $array, callable $predicate) : bool
Parameters
- $array : iterable<string|int, mixed>
- $predicate : callable
Tags
Return values
boolfilter()
Returns a new array containing all key-value pairs matching the given $predicate.
public
static filter(array<string|int, mixed> $array, callable $predicate) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $predicate : callable
Tags
Return values
array<string|int, mixed>first()
Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
public
static first(array<string|int, mixed> $array[, callable|null $predicate = null ][, callable|null $else = null ]) : V|null
Parameters
- $array : array<string|int, mixed>
- $predicate : callable|null = null
- $else : callable|null = null
Tags
Return values
V|nullfirstKey()
Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
public
static firstKey(array<string|int, mixed> $array[, callable|null $predicate = null ]) : K|null
Parameters
- $array : array<string|int, mixed>
- $predicate : callable|null = null
Tags
Return values
K|nullflatten()
Transforms multidimensional array to flat array.
public
static flatten(array<string|int, mixed> $array[, bool $preserveKeys = false ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $preserveKeys : bool = false
Return values
array<string|int, mixed>get()
Returns item from array. If it does not exist, it throws an exception, unless a default value is set.
public
static get(array<string|int, T> $array, (array-key)|array<string|int, string|int> $key[, T|null $default = null ]) : T|null
Parameters
- $array : array<string|int, T>
- $key : (array-key)|array<string|int, string|int>
- $default : T|null = null
Tags
Return values
T|nullgetKeyOffset()
Returns zero-indexed position of given array key. Returns null if key is not found.
public
static getKeyOffset(array<string|int, mixed> $array, string|int $key) : int|null
Parameters
- $array : array<string|int, mixed>
- $key : string|int
Return values
int|nullgetRef()
Returns reference to array item. If the index does not exist, new one is created with value null.
public
static & getRef(array<string|int, T> &$array, (array-key)|array<string|int, string|int> $key) : T|null
Parameters
- $array : array<string|int, T>
- $key : (array-key)|array<string|int, string|int>
Tags
Return values
T|nullgrep()
Returns only those array items, which matches a regular expression $pattern.
public
static grep(array<string|int, string> $array, string $pattern[, bool|int $invert = false ]) : array<string|int, string>
Parameters
- $array : array<string|int, string>
- $pattern : string
- $invert : bool|int = false
Return values
array<string|int, string>insertAfter()
Inserts the contents of the $inserted array into the $array before the $key.
public
static insertAfter(array<string|int, mixed> &$array, string|int|null $key, array<string|int, mixed> $inserted) : void
If $key is null (or does not exist), it is inserted at the end.
Parameters
- $array : array<string|int, mixed>
- $key : string|int|null
- $inserted : array<string|int, mixed>
insertBefore()
Inserts the contents of the $inserted array into the $array immediately after the $key.
public
static insertBefore(array<string|int, mixed> &$array, string|int|null $key, array<string|int, mixed> $inserted) : void
If $key is null (or does not exist), it is inserted at the beginning.
Parameters
- $array : array<string|int, mixed>
- $key : string|int|null
- $inserted : array<string|int, mixed>
invoke()
Invokes all callbacks and returns array of results.
public
static invoke(array<string|int, callable> $callbacks, mixed ...$args) : array<string|int, mixed>
Parameters
- $callbacks : array<string|int, callable>
- $args : mixed
Return values
array<string|int, mixed>invokeMethod()
Invokes method on every object in an array and returns array of results.
public
static invokeMethod(array<string|int, object> $objects, string $method, mixed ...$args) : array<string|int, mixed>
Parameters
- $objects : array<string|int, object>
- $method : string
- $args : mixed
Return values
array<string|int, mixed>isList()
Checks if the array is indexed in ascending order of numeric keys from zero, a.k.a list.
public
static isList(mixed $value) : ($value is list ? true : false)
Parameters
- $value : mixed
Return values
($value is list ? true : false)last()
Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null.
public
static last(array<string|int, mixed> $array[, callable|null $predicate = null ][, callable|null $else = null ]) : V|null
Parameters
- $array : array<string|int, mixed>
- $predicate : callable|null = null
- $else : callable|null = null
Tags
Return values
V|nulllastKey()
Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
public
static lastKey(array<string|int, mixed> $array[, callable|null $predicate = null ]) : K|null
Parameters
- $array : array<string|int, mixed>
- $predicate : callable|null = null
Tags
Return values
K|nullmap()
Returns an array containing the original keys and results of applying the given transform function to each element.
public
static map(iterable<string|int, mixed> $array, callable $transformer) : array<string|int, mixed>
Parameters
- $array : iterable<string|int, mixed>
- $transformer : callable
Tags
Return values
array<string|int, mixed>mapWithKeys()
Returns an array containing new keys and values generated by applying the given transform function to each element.
public
static mapWithKeys(array<string|int, mixed> $array, callable $transformer) : array<string|int, mixed>
If the function returns null, the element is skipped.
Parameters
- $array : array<string|int, mixed>
- $transformer : callable
Tags
Return values
array<string|int, mixed>mergeTree()
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.
public
static mergeTree(array<string|int, T1> $array1, array<string|int, T2> $array2) : array<string|int, T1|T2>
Parameters
- $array1 : array<string|int, T1>
- $array2 : array<string|int, T2>
Tags
Return values
array<string|int, T1|T2>normalize()
Normalizes array to associative array. Replace numeric keys with their values, the new value will be $filling.
public
static normalize(array<string|int, mixed> $array[, mixed $filling = null ]) : array<string|int, mixed>
Parameters
- $array : array<string|int, mixed>
- $filling : mixed = null
Return values
array<string|int, mixed>pick()
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.
public
static pick(array<string|int, T> &$array, string|int $key[, T|null $default = null ]) : T|null
Parameters
- $array : array<string|int, T>
- $key : string|int
- $default : T|null = null
Tags
Return values
T|nullrenameKey()
Renames key in array.
public
static renameKey(array<string|int, mixed> &$array, string|int $oldKey, string|int $newKey) : bool
Parameters
- $array : array<string|int, mixed>
- $oldKey : string|int
- $newKey : string|int
Return values
boolsearchKey()
public
static searchKey(array<string|int, mixed> $array, mixed $key) : int|null
use getKeyOffset()
Parameters
- $array : array<string|int, mixed>
- $key : mixed
Return values
int|nullsome()
Tests whether at least one element in the array passes the test implemented by the provided function.
public
static some(iterable<string|int, mixed> $array, callable $predicate) : bool
Parameters
- $array : iterable<string|int, mixed>
- $predicate : callable
Tags
Return values
booltoKey()
Converts value to array key.
public
static toKey(mixed $value) : int|string
Parameters
- $value : mixed
Return values
int|stringtoObject()
Copies the elements of the $array array to the $object object and then returns it.
public
static toObject(iterable<string|int, mixed> $array, T $object) : T
Parameters
- $array : iterable<string|int, mixed>
- $object : T
Tags
Return values
Twrap()
Returns copy of the $array where every item is converted to string and prefixed by $prefix and suffixed by $suffix.
public
static wrap(array<string|int, string> $array[, string $prefix = '' ][, string $suffix = '' ]) : array<string|int, string>
Parameters
- $array : array<string|int, string>
- $prefix : string = ''
- $suffix : string = ''
Return values
array<string|int, string>__construct()
Class is static and cannot be instantiated.
private
__construct() : mixed