abstract class Arrays (View source)

Some array functions to help with common tasks

Methods

static array
integerExplode(string $delimiter, string $string)

Explodes a $string delimited by $delimiter and passes each item in the array through intval().

static array
trimExplode(string $delimiter, string $string, bool $onlyNonEmptyValues = true)

Explodes a string and trims all values for whitespace in the ends.

static array
arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, bool $dontAddNewKeys = false, bool $emptyValuesOverride = true)

Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($firstArray) with the values of the second array ($secondArray) in case of identical keys, ie. keeping the values of the second.

static array
arrayMergeRecursiveOverruleWithCallback(array $firstArray, array $secondArray, Closure $toArray, Closure|null $overrideFirst = null)

Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($firstArray) with the values of the second array ($secondArray) In case of identical keys, ie. keeping the values of the second. The given $toArray closure will be used if one of the two array keys contains an array and the other not. It should return an array.

static bool
containsMultipleTypes(array $array)

Returns true if the given array contains elements of varying types

static mixed
array_reduce(array $array, string $function, mixed $initial = null)

Replacement for array_reduce that allows any type for $initial (instead of only integer)

static mixed
getValueByPath(array $array, array|string $path)

Returns the value of a nested array by following the specifed path.

static array|ArrayAccess
setValueByPath(array|ArrayAccess $subject, array|string $path, mixed $value)

Sets the given value in a nested array or object by following the specified path.

static array
unsetValueByPath(array $array, array|string $path)

Unsets an element/part of a nested array by following the specified path.

static bool
sortKeysRecursively(array $array, int $sortFlags = \SORT_REGULAR)

Sorts multidimensional arrays by recursively calling ksort on its elements.

static array
convertObjectToArray(mixed $subject)

Recursively convert an object hierarchy into an associative array.

static array
removeEmptyElementsRecursively(array $array)

Recursively removes empty array elements.

Details

static array integerExplode(string $delimiter, string $string)

Explodes a $string delimited by $delimiter and passes each item in the array through intval().

Corresponds to explode(), but with conversion to integers for all values.

Parameters

string $delimiter

Delimiter string to explode with

string $string

The string to explode

Return Value

array

Exploded values, all converted to integers

static array trimExplode(string $delimiter, string $string, bool $onlyNonEmptyValues = true)

Explodes a string and trims all values for whitespace in the ends.

If $onlyNonEmptyValues is set, then all blank ('') values are removed.

Parameters

string $delimiter

Delimiter string to explode with

string $string

The string to explode

bool $onlyNonEmptyValues

If disabled, even empty values (='') will be set in output

Return Value

array

Exploded values

static array arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, bool $dontAddNewKeys = false, bool $emptyValuesOverride = true)

Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($firstArray) with the values of the second array ($secondArray) in case of identical keys, ie. keeping the values of the second.

Parameters

array $firstArray

First array

array $secondArray

Second array, overruling the first array

bool $dontAddNewKeys

If set, keys that are NOT found in $firstArray (first array) will not be set. Thus only existing value can/will be overruled from second array.

bool $emptyValuesOverride

If set (which is the default), values from $secondArray will overrule if they are empty (according to PHP's empty() function)

Return Value

array

Resulting array where $secondArray values has overruled $firstArray values

static array arrayMergeRecursiveOverruleWithCallback(array $firstArray, array $secondArray, Closure $toArray, Closure|null $overrideFirst = null)

Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values in the first array ($firstArray) with the values of the second array ($secondArray) In case of identical keys, ie. keeping the values of the second. The given $toArray closure will be used if one of the two array keys contains an array and the other not. It should return an array.

Parameters

array $firstArray

First array

array $secondArray

Second array, overruling the first array

Closure $toArray

The given callable will get a value that is not an array and has to return an array. This is to allow custom merging of simple types with (sub) arrays

Closure|null $overrideFirst

The given callable will determine whether the value of the first array should be overridden. It should have the following signature $callable($key, ?array $firstValue = null, ?array $secondValue = null): bool

Return Value

array

Resulting array where $secondArray values has overruled $firstArray values

static bool containsMultipleTypes(array $array)

Returns true if the given array contains elements of varying types

Parameters

array $array

Return Value

bool

static mixed array_reduce(array $array, string $function, mixed $initial = null)

Replacement for array_reduce that allows any type for $initial (instead of only integer)

Parameters

array $array

the array to reduce

string $function

the reduce function with the same order of parameters as in the native array_reduce (i.e. accumulator first, then current array element)

mixed $initial

the initial accumulator value

Return Value

mixed

static mixed getValueByPath(array $array, array|string $path)

Returns the value of a nested array by following the specifed path.

Parameters

array $array

The array to traverse

array|string $path

The path to follow. Either a simple array of keys or a string in the format 'foo.bar.baz'

Return Value

mixed

The value found, NULL if the path didn't exist (note there is no way to distinguish between a found NULL value and "path not found")

Exceptions

InvalidArgumentException

static array|ArrayAccess setValueByPath(array|ArrayAccess $subject, array|string $path, mixed $value)

Sets the given value in a nested array or object by following the specified path.

Parameters

array|ArrayAccess $subject

The array or ArrayAccess instance to work on

array|string $path

The path to follow. Either a simple array of keys or a string in the format 'foo.bar.baz'

mixed $value

The value to set

Return Value

array|ArrayAccess

The modified array or object

Exceptions

InvalidArgumentException

static array unsetValueByPath(array $array, array|string $path)

Unsets an element/part of a nested array by following the specified path.

Parameters

array $array

The array

array|string $path

The path to follow. Either a simple array of keys or a string in the format 'foo.bar.baz'

Return Value

array

The modified array

Exceptions

InvalidArgumentException

static bool sortKeysRecursively(array $array, int $sortFlags = \SORT_REGULAR)

Sorts multidimensional arrays by recursively calling ksort on its elements.

Parameters

array $array

the array to sort

int $sortFlags

may be used to modify the sorting behavior using these values (see http://www.php.net/manual/en/function.sort.php)

Return Value

bool

true on success, false on failure

See also

asort()

static array convertObjectToArray(mixed $subject)

Recursively convert an object hierarchy into an associative array.

Parameters

mixed $subject

An object or array of objects

Return Value

array

The subject represented as an array

Exceptions

InvalidArgumentException

static array removeEmptyElementsRecursively(array $array)

Recursively removes empty array elements.

Parameters

array $array

Return Value

array

the modified array