ArrayHelper
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
Concatenate arrays or values to a new array
Join values of an array with a separator
Extract a portion of an indexed array
Returns an array in reverse order
Get the array keys
Get the length of an array
Check if an array is empty
Get the first element of an array
Get the last element of an array
Returns the first index at which a given element can be found in the array, or -1 if it is not present
Picks a random element from the array
Sorts an array
Sort an array by key
Shuffle an array
Removes duplicate values from an array
Removes the last element from an array
Insert one or more elements at the end of an array
Remove the first element of an array
Insert one or more elements at the beginning of an array
Replaces a range of an array by the given replacements
Exchanges all keys with their associated values in an array
Create an array containing a range of elements
Set the specified key in the the array
Apply the callback to each element of the array, passing each element and key as arguments
Apply the callback to each element of the array and accumulate a single value
Filter an array by a test given as the callback, passing each element and key as arguments
Check if at least one element in an array passes a test given by the calback, passing each element and key as arguments
Check if all elements in an array pass a test given by the calback, passing each element and key as arguments
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
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
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.
array
ksort(iterable $array)
Sort an array by key
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
array
unique(iterable $array)
Removes duplicate values from an 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.
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)
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.
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)
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')
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.
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.
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
Examples::
Array.map([1, 2, 3, 4], x => x x) Array.map([1, 2, 3, 4], (x, index) => x index)
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
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']
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
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
bool
allowsCallOfMethod(string $methodName)
All methods are considered safe