class ArrayHelper implements ProtectedContextAwareInterface (View source)

Array helpers for Eel contexts

The implementation uses the JavaScript specificiation where applicable, including EcmaScript 6 proposals.

See https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array for a documentation and specification of the JavaScript implementation.

Methods

array
concat(iterable|mixed $array1, iterable|mixed $array2, iterable|mixed $array_ = null)

Concatenate arrays or values to a new array

string
join(iterable $array, string $separator = ',')

Join values of an array with a separator

array
slice(iterable $array, int $begin, int $end = null)

Extract a portion of an indexed array

array
reverse(iterable $array)

Returns an array in reverse order

array
keys(iterable $array)

Get the array keys

int
length(iterable $array)

Get the length of an array

bool
isEmpty(iterable $array)

Check if an array is empty

mixed
first(iterable $array)

Get the first element of an array

mixed
last(iterable $array)

Get the last element of an array

int
indexOf(iterable $array, mixed $searchElement, int $fromIndex = null)

Returns the first index at which a given element can be found in the array, or -1 if it is not present

mixed
random(iterable $array)

Picks a random element from the array

array
sort(iterable $array)

Sorts an array

array
ksort(iterable $array)

Sort an array by key

array
shuffle(iterable $array, bool $preserveKeys = true)

Shuffle an array

array
unique(iterable $array)

Removes duplicate values from an array

array
pop(iterable $array)

Removes the last element from an array

array
push(iterable|scalar|null $array, mixed $element)

Insert one or more elements at the end of an array

array
shift(iterable $array)

Remove the first element of an array

array
unshift(iterable $array, mixed $element)

Insert one or more elements at the beginning of an array

array
splice(iterable $array, int $offset, int $length = 1, mixed $replacements = null)

Replaces a range of an array by the given replacements

array
flip(iterable $array)

Exchanges all keys with their associated values in an array

array
range(mixed $start, mixed $end, int $step = 1)

Create an array containing a range of elements

array
set(iterable $array, string|int $key, mixed $value)

Set the specified key in the the array

array
map(iterable $array, callable $callback)

Apply the callback to each element of the array, passing each element and key as arguments

mixed
reduce(iterable $array, callable $callback, mixed $initialValue = null)

Apply the callback to each element of the array and accumulate a single value

array
filter(iterable $array, callable $callback = null)

Filter an array by a test given as the callback, passing each element and key as arguments

bool
some(iterable $array, callable $callback)

Check if at least one element in an array passes a test given by the calback, passing each element and key as arguments

bool
every(iterable $array, callable $callback)

Check if all elements in an array pass a test given by the calback, passing each element and key as arguments

bool
allowsCallOfMethod(string $methodName)

All methods are considered safe

Details

array concat(iterable|mixed $array1, iterable|mixed $array2, iterable|mixed $array_ = null)

Concatenate arrays or values to a new array

Parameters

iterable|mixed $array1

First array or value

iterable|mixed $array2

Second array or value

iterable|mixed $array_

Optional variable list of additional arrays / values

Return Value

array

The array with concatenated arrays or values

string join(iterable $array, string $separator = ',')

Join values of an array with a separator

Parameters

iterable $array

Array with values to join

string $separator

A separator for the values

Return Value

string

A string with the joined values separated by the separator

array slice(iterable $array, int $begin, int $end = null)

Extract a portion of an indexed array

Parameters

iterable $array

The array (with numeric indices)

int $begin
int $end

Return Value

array

array reverse(iterable $array)

Returns an array in reverse order

Parameters

iterable $array

The array

Return Value

array

array keys(iterable $array)

Get the array keys

Parameters

iterable $array

The array

Return Value

array

int length(iterable $array)

Get the length of an array

Parameters

iterable $array

The array

Return Value

int

bool isEmpty(iterable $array)

Check if an array is empty

Parameters

iterable $array

The array

Return Value

bool

true if the array is empty

mixed first(iterable $array)

Get the first element of an array

Parameters

iterable $array

The array

Return Value

mixed

mixed last(iterable $array)

Get the last element of an array

Parameters

iterable $array

The array

Return Value

mixed

int indexOf(iterable $array, mixed $searchElement, int $fromIndex = null)

Returns the first index at which a given element can be found in the array, or -1 if it is not present

Parameters

iterable $array

The array

mixed $searchElement

The element value to find

int $fromIndex

Position in the array to start the search.

Return Value

int

mixed random(iterable $array)

Picks a random element from the array

Parameters

iterable $array

Return Value

mixed

A random entry or null if the array is empty

array sort(iterable $array)

Sorts an array

The sorting is done first by numbers, then by characters.

Internally natsort() is used as it most closely resembles javascript's sort(). Because there are no real associative arrays in Javascript, keys of the array will be preserved.

Parameters

iterable $array

Return Value

array

The sorted array

array ksort(iterable $array)

Sort an array by key

Parameters

iterable $array

The array to sort

Return Value

array

The sorted array

array shuffle(iterable $array, bool $preserveKeys = true)

Shuffle an array

Randomizes entries an array with the option to preserve the existing keys. When this option is set to false, all keys will be replaced

Parameters

iterable $array
bool $preserveKeys

Wether to preserve the keys when shuffling the array

Return Value

array

The shuffled array

array unique(iterable $array)

Removes duplicate values from an array

Parameters

iterable $array

The input array

Return Value

array

The filtered array.

array pop(iterable $array)

Removes the last element from an array

Note: This differs from the JavaScript behavior of Array.pop which will return the popped element.

An empty array will result in an empty array again.

Parameters

iterable $array

Return Value

array

The array without the last element

array push(iterable|scalar|null $array, mixed $element)

Insert one or more elements at the end of an array

Allows to push multiple elements at once::

Array.push(array, e1, e2)

Parameters

iterable|scalar|null $array
mixed $element

Return Value

array

The array with the inserted elements

array shift(iterable $array)

Remove the first element of an array

Note: This differs from the JavaScript behavior of Array.shift which will return the shifted element.

An empty array will result in an empty array again.

Parameters

iterable $array

Return Value

array

The array without the first element

array unshift(iterable $array, mixed $element)

Insert one or more elements at the beginning of an array

Allows to insert multiple elements at once::

Array.unshift(array, e1, e2)

Parameters

iterable $array
mixed $element

Return Value

array

The array with the inserted elements

array splice(iterable $array, int $offset, int $length = 1, mixed $replacements = null)

Replaces a range of an array by the given replacements

Allows to give multiple replacements at once::

Array.splice(array, 3, 2, 'a', 'b')

Parameters

iterable $array
int $offset

Index of the first element to remove

int $length

Number of elements to remove

mixed $replacements

Elements to insert instead of the removed range

Return Value

array

The array with removed and replaced elements

array flip(iterable $array)

Exchanges all keys with their associated values in an array

Note that the values of array need to be valid keys, i.e. they need to be either int or string. If a value has several occurrences, the latest key will be used as its value, and all others will be lost.

Parameters

iterable $array

Return Value

array

The array with flipped keys and values

array range(mixed $start, mixed $end, int $step = 1)

Create an array containing a range of elements

If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1.

Parameters

mixed $start

First value of the sequence.

mixed $end

The sequence is ended upon reaching the end value.

int $step

The increment between items, will default to 1.

Return Value

array

Array of elements from start to end, inclusive.

array set(iterable $array, string|int $key, mixed $value)

Set the specified key in the the array

Parameters

iterable $array
string|int $key

the key that should be set

mixed $value

the value to assign to the key

Return Value

array

The modified array.

array map(iterable $array, callable $callback)

Apply the callback to each element of the array, passing each element and key as arguments

Examples::

Array.map([1, 2, 3, 4], x => x x) Array.map([1, 2, 3, 4], (x, index) => x index)

Parameters

iterable $array

Array of elements to map

callable $callback

Callback to apply for each element, current value and key will be passed as arguments

Return Value

array

The array with callback applied, keys will be preserved

mixed reduce(iterable $array, callable $callback, mixed $initialValue = null)

Apply the callback to each element of the array and accumulate a single value

Examples::

Array.reduce([1, 2, 3, 4], (accumulator, currentValue) => accumulator + currentValue) // == 10 Array.reduce([1, 2, 3, 4], (accumulator, currentValue) => accumulator + currentValue, 1) // == 11

Parameters

iterable $array

Array of elements to reduce to a value

callable $callback

Callback for accumulating values, accumulator, current value and key will be passed as arguments

mixed $initialValue

Initial value, defaults to first item in array and callback starts with second entry

Return Value

mixed

array filter(iterable $array, callable $callback = null)

Filter an array by a test given as the callback, passing each element and key as arguments

Examples:

Array.filter([1, 2, 3, 4], x => x % 2 == 0) // == [2, 4] Array.filter(['foo', 'bar', 'baz'], (x, index) => index < 2) // == ['foo', 'bar']

Parameters

iterable $array

Array of elements to filter

callable $callback

Callback for testing if an element should be included in the result, current value and key will be passed as arguments

Return Value

array

The array with elements where callback returned true

bool some(iterable $array, callable $callback)

Check if at least one element in an array passes a test given by the calback, passing each element and key as arguments

Example::

Array.some([1, 2, 3, 4], x => x % 2 == 0) // == true Array.some([1, 2, 3, 4], x => x > 4) // == false

Parameters

iterable $array

Array of elements to test

callable $callback

Callback for testing elements, current value and key will be passed as arguments

Return Value

bool

True if at least one element passed the test

bool every(iterable $array, callable $callback)

Check if all elements in an array pass a test given by the calback, passing each element and key as arguments

Example::

Array.every([1, 2, 3, 4], x => x % 2 == 0) // == false Array.every([2, 4, 6, 8], x => x % 2) // == true

Parameters

iterable $array

Array of elements to test

callable $callback

Callback for testing elements, current value and key will be passed as arguments

Return Value

bool

True if all elements passed the test

bool allowsCallOfMethod(string $methodName)

All methods are considered safe

Parameters

string $methodName

Return Value

bool