toolkit

Arr

Array helpers: map, filter, find, sort, pluck and friends.

Arr:: Phlat\Toolkit\Arr 43 methods

Inspection 8

  • isEmpty Whether the array has no elements.
  • count The number of elements.
  • has Checks by value.
  • hasKey Whether $key exists, even with a null value.
  • isList True if the array has sequential integer keys starting from 0 (a list).
  • first Returns the first element, or null if empty.
  • last Returns the last element, or null if empty.
  • lastKey The last key, or null when empty.

Extraction 6

  • keys The keys, as a list.
  • values The values, re-indexed as a list.
  • take Returns the first $count elements.
  • slice A portion of $length elements from $offset; string keys are kept.
  • chunk Splits into lists of $size elements.
  • pluck Extracts one key from each item in an array of arrays.

Transformation 13

  • range The integers from $start to $end, $step apart.
  • combine Pairs $keys with $values, which must be the same length.
  • map Applies $fn to each value, keeping keys.
  • random One value picked at random.
  • filter Filters by callback, re-indexed unless $preserveKeys is true (needed for associative arrays whose string keys carry meaning).
  • filterKeys Filters by key; callback receives the key.
  • partition Splits into [matches, non-matches] based on a callback.
  • reduce Folds the values into one, starting from $initial.
  • unique The distinct values, re-indexed.
  • reverse The elements in reverse order.
  • flip Swaps keys and values.
  • flatten Flattens one level of nesting.
  • setPath Returns a copy of $array with $value set at the nested $keys path, creating missing levels.

Searching 5

  • find Returns the first element matching the callback, or null.
  • findKey Returns the key of the first matching element, or null.
  • search Returns the key of the first element strictly equal to $value, or null.
  • every Whether every element passes the callback; true for an empty array.
  • some Whether at least one element passes the callback.

Sorting 4

  • sort Returns a sorted copy, re-indexed.
  • sortKeys Returns a copy sorted by key, keys preserved.
  • sortBy Returns a copy sorted by a comparison callback, re-indexed.
  • sortWithKeys Returns a copy sorted by a comparison callback, preserving string keys.

Combination 7

  • keyBy Returns an associative array keyed by the result of $fn applied to each element.
  • merge Merges arrays: string keys from a later array win, numeric keys are appended.
  • mergeRecursive Deep merge for layered settings: maps merge key by key, anything else (a list included) is replaced wholesale by the later layer, as in JSON Merge Patch (RFC 7386), so a layer can shrink a list as well as grow it.
  • prepend Prepends values to the front of the array.
  • append Appends values to the end of the array.
  • only Returns only the entries whose keys are in $keys.
  • except Returns the array with the given keys removed.

Inspection

Arr::isEmpty(array $array): bool

Whether the array has no elements.

Parameters (1)
$array array

Returns bool

phlat/src/Toolkit/Arr.php:17
Arr::count(array $array): int

The number of elements.

Parameters (1)
$array array

Returns int

phlat/src/Toolkit/Arr.php:23

has

Arr::has(array $array, mixed $value, bool $strict = false): bool

Checks by value.

Use $strict = true for type-safe comparison.

Parameters (3)
$array array
$value mixed
$strict bool = false

Returns bool

phlat/src/Toolkit/Arr.php:29
Arr::hasKey(array $array, string|int $key): bool

Whether $key exists, even with a null value.

Parameters (2)
$array array
$key string|int

Returns bool

phlat/src/Toolkit/Arr.php:35
Arr::isList(array $array): bool

True if the array has sequential integer keys starting from 0 (a list).

Parameters (1)
$array array

Returns bool

phlat/src/Toolkit/Arr.php:41
Arr::first(array $array): mixed

Returns the first element, or null if empty.

Parameters (1)
$array array

Returns mixed

phlat/src/Toolkit/Arr.php:47
Arr::last(array $array): mixed

Returns the last element, or null if empty.

Parameters (1)
$array array

Returns mixed

phlat/src/Toolkit/Arr.php:53
Arr::lastKey(array $array): string|int|null

The last key, or null when empty.

Parameters (1)
$array array

Returns string|int|null

phlat/src/Toolkit/Arr.php:59

Extraction

Arr::keys(array $array): array

The keys, as a list.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:67
Arr::values(array $array): array

The values, re-indexed as a list.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:73
Arr::take(array $array, int $count): array

Returns the first $count elements.

Parameters (2)
$array array
$count int

Returns array

phlat/src/Toolkit/Arr.php:79
Arr::slice(array $array, int $offset, ?int $length = null): array

A portion of $length elements from $offset; string keys are kept.

Parameters (3)
$array array
$offset int
$length ?int = null

Returns array

phlat/src/Toolkit/Arr.php:85
Arr::chunk(array $array, int $size): array

Splits into lists of $size elements.

Parameters (2)
$array array
$size int

Returns array

phlat/src/Toolkit/Arr.php:91
Arr::pluck(array $array, string $key): array

Extracts one key from each item in an array of arrays.

Parameters (2)
$array array
$key string

Returns array

phlat/src/Toolkit/Arr.php:97

Transformation

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

The integers from $start to $end, $step apart.

Parameters (3)
$start int
$end int
$step int = 1

Returns array

phlat/src/Toolkit/Arr.php:105
Arr::combine(array $keys, array $values): array

Pairs $keys with $values, which must be the same length.

Parameters (2)
$keys array
$values array

Returns array

phlat/src/Toolkit/Arr.php:111

map

Arr::map(array $array, callable $fn): array

Applies $fn to each value, keeping keys.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:117
Arr::random(array $values): mixed

One value picked at random.

Parameters (1)
$values array

Returns mixed

phlat/src/Toolkit/Arr.php:123
Arr::filter(array $array, ?callable $fn = null, bool $preserveKeys = false): array

Filters by callback, re-indexed unless $preserveKeys is true (needed for associative arrays whose string keys carry meaning).

Parameters (3)
$array array
$fn ?callable = null
$preserveKeys bool = false

Returns array

phlat/src/Toolkit/Arr.php:130
Arr::filterKeys(array $array, callable $fn): array

Filters by key; callback receives the key.

Preserves string keys.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:137
Arr::partition(array $array, callable $fn): array

Splits into [matches, non-matches] based on a callback.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:143
Arr::reduce(array $array, callable $fn, mixed $initial = null): mixed

Folds the values into one, starting from $initial.

Parameters (3)
$array array
$fn callable
$initial mixed = null

Returns mixed

phlat/src/Toolkit/Arr.php:157
Arr::unique(array $array): array

The distinct values, re-indexed.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:163
Arr::reverse(array $array): array

The elements in reverse order.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:169
Arr::flip(array $array): array

Swaps keys and values.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:175
Arr::flatten(array $array): array

Flattens one level of nesting.

Parameters (1)
$array array

Returns array

phlat/src/Toolkit/Arr.php:181
Arr::setPath(array $array, array $keys, mixed $value): array

Returns a copy of $array with $value set at the nested $keys path, creating missing levels.

Parameters (3)
$array array
$keys array
$value mixed

Returns array

phlat/src/Toolkit/Arr.php:190

Searching

Arr::find(array $array, callable $fn): mixed

Returns the first element matching the callback, or null.

Parameters (2)
$array array
$fn callable

Returns mixed

phlat/src/Toolkit/Arr.php:200
Arr::findKey(array $array, callable $fn): string|int|null

Returns the key of the first matching element, or null.

Parameters (2)
$array array
$fn callable

Returns string|int|null

phlat/src/Toolkit/Arr.php:210
Arr::every(array $array, callable $fn): bool

Whether every element passes the callback; true for an empty array.

Parameters (2)
$array array
$fn callable

Returns bool

phlat/src/Toolkit/Arr.php:227
Arr::some(array $array, callable $fn): bool

Whether at least one element passes the callback.

Parameters (2)
$array array
$fn callable

Returns bool

phlat/src/Toolkit/Arr.php:233

Sorting

Arr::sort(array $array, int $flags = SORT_REGULAR): array

Returns a sorted copy, re-indexed.

Parameters (2)
$array array
$flags int = SORT_REGULAR

Returns array

phlat/src/Toolkit/Arr.php:241
Arr::sortKeys(array $array, int $flags = SORT_REGULAR): array

Returns a copy sorted by key, keys preserved.

Parameters (2)
$array array
$flags int = SORT_REGULAR

Returns array

phlat/src/Toolkit/Arr.php:248
Arr::sortBy(array $array, callable $fn): array

Returns a copy sorted by a comparison callback, re-indexed.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:255
Arr::sortWithKeys(array $array, callable $fn): array

Returns a copy sorted by a comparison callback, preserving string keys.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:262

Combination

Arr::keyBy(array $array, callable $fn): array

Returns an associative array keyed by the result of $fn applied to each element.

Parameters (2)
$array array
$fn callable

Returns array

phlat/src/Toolkit/Arr.php:271
Arr::merge(array ...$arrays): array

Merges arrays: string keys from a later array win, numeric keys are appended.

Parameters (1)
...$arrays array

Returns array

phlat/src/Toolkit/Arr.php:281
Arr::mergeRecursive(array ...$arrays): array

Deep merge for layered settings: maps merge key by key, anything else (a list included) is replaced wholesale by the later layer, as in JSON Merge Patch (RFC 7386), so a layer can shrink a list as well as grow it.

Parameters (1)
...$arrays array

Returns array

phlat/src/Toolkit/Arr.php:287
Arr::prepend(array $array, mixed ...$values): array

Prepends values to the front of the array.

Parameters (2)
$array array
...$values mixed

Returns array

phlat/src/Toolkit/Arr.php:307
Arr::append(array $array, mixed ...$values): array

Appends values to the end of the array.

Parameters (2)
$array array
...$values mixed

Returns array

phlat/src/Toolkit/Arr.php:314
Arr::only(array $array, array $keys): array

Returns only the entries whose keys are in $keys.

Parameters (2)
$array array
$keys array

Returns array

phlat/src/Toolkit/Arr.php:321
Arr::except(array $array, array $keys): array

Returns the array with the given keys removed.

Parameters (2)
$array array
$keys array

Returns array

phlat/src/Toolkit/Arr.php:327