Documentation

Arr
in package

FinalYes

Utilities for arrays or iterables.

Tags
since
1.0

Table of Contents

Methods

find()  : array{k: string|int|null, v: mixed}
Searches for a mapping in the array whose key is one of $keys, returns the key and the value or null/null (if not found) as an array.
find_by()  : mixed
Searches for a value in the array, based on the predicate, returning it (or its key) or null, when not found.
first()  : array{k: string|int|null, v: mixed}
Returns the first mapping (element) of an array as key and value in an array, or null/null if the array is empty.
kvjoin()  : string
Like implode()/join() in legacy syntax, but outputs the keys too and takes an additional parameter `$kv_sep`.
pick()  : array<string|int, mixed>
Returns a new array with only the key => value mappings left, whose keys are in $keys.
reduce()  : mixed
Like {@link \array_reduce()}, but accepts an iterable and the callback passes the key of the element, too.

Methods

find()

Searches for a mapping in the array whose key is one of $keys, returns the key and the value or null/null (if not found) as an array.

public static find(array<string|int, mixed> $array, string|int ...$ks) : array{k: string|int|null, v: mixed}

Array destructuring is a very convenient way to handle the result: ['k' => $key, 'v' => $value] = Arr::find($array, 'key1', 'key2')

Parameters
$array : array<string|int, mixed>

the source array

$ks : string|int

the keys

Tags
since
1.0
Return values
array{k: string|int|null, v: mixed}

['k' => <key>/null, 'v' => <value>/null]

find_by()

Searches for a value in the array, based on the predicate, returning it (or its key) or null, when not found.

public static find_by(array<string|int, mixed> $array, callable(mixed, Array): bool $predicate[, bool $return_key = false ]) : mixed
Parameters
$array : array<string|int, mixed>

the source array

$predicate : callable(mixed, Array): bool

called with every value (until found)

$return_key : bool = false

whether to return the key instead of the value

Tags
since
1.0
Return values
mixed

the value or the key for a found item, null otherwise

first()

Returns the first mapping (element) of an array as key and value in an array, or null/null if the array is empty.

public static first(iterable<string|int, mixed> $iterable) : array{k: string|int|null, v: mixed}

Array destructuring is a very convenient way to handle the result: ['k' => $key, 'v' => $value] = Arr::first($array)

Parameters
$iterable : iterable<string|int, mixed>

the source iterable

Tags
since
1.0
Return values
array{k: string|int|null, v: mixed}

['k' => <key>/null, 'v' => <value>/null]

kvjoin()

Like implode()/join() in legacy syntax, but outputs the keys too and takes an additional parameter `$kv_sep`.

public static kvjoin(iterable<string|int, mixed> $data[, string $sep = ', ' ][, string $kv_sep = '=' ]) : string
Parameters
$data : iterable<string|int, mixed>

the elements to join

$sep : string = ', '

the separator of elements

$kv_sep : string = '='

the separator for key and value

Tags
since
1.0
Return values
string

pick()

Returns a new array with only the key => value mappings left, whose keys are in $keys.

public static pick(array<string|int, mixed> $array, string|int ...$keys) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>

the source array

$keys : string|int

the keys for the values to retain

Tags
since
1.0
Return values
array<string|int, mixed>

the resulting array

reduce()

Like {@link \array_reduce()}, but accepts an iterable and the callback passes the key of the element, too.

public static reduce(iterable<string|int, mixed> $iterable, callable(mixed $carry, mixed $value, Array $key): mixed $callback[, mixed $initial = new self() ][, bool $on_empty = false ]) : mixed
Parameters
$iterable : iterable<string|int, mixed>

the elements to reduce

$callback : callable(mixed $carry, mixed $value, Array $key): mixed
$initial : mixed = new self()

an optional initial value. if not passed, the first element is taken as the initial value and the $callback is not called for it

$on_empty : bool = false

(since 1.2) whether to return the initial value iff the iterable is empty

Tags
throws
RuntimeException

when the iterable is empty, no initial value was passed and no_empty was not given

since
1.0
Return values
mixed

the reduced value, the initial value or the on_empty value (if passed)


        
On this page

Search results