Usephul

array.php

Tags
author

Alwin Garside alwin@garsi.de

copyright

2025 The Empaphy Project

license

MIT

Table of Contents

Functions

array_exclude()  : array<string|int, mixed>
Exclude from an array all the elements that match the provided values.
array_extract()  : array<string|int, mixed>
Extract from an array all the elements of the input array matching the provided values.
array_get()  : TArray : mixed)
Retrieve an element from an array that can be located several levels deep.
array_interchange()  : array<string|int, mixed>
Interchange the values of two elements in an array.
array_key_types()  : ($array is array{} ? array{} : ($array is array ? array{integer: true} : ($array is array ? array{string: true} : array{integer: true, string: true})))
Inspects the types of keys used in an array.
array_omit()  : array<string|int, mixed>
Create a copy of the input array while omiting specific keys.
array_pick()  : array<string|int, mixed>
Create a new array by picking elements from the input array corresponding to a specific set of keys.
array_remap()  : array<string|int, mixed>
Applies a (generator) callback to the elements of a given array, allowing the remapping of its keys in the process.
array_split()  : TArray> : array[]))
Split an array by a value.
array_zip()  : array<int, TValue}>
Perform a zip operation on multiple arrays.

Functions

array_exclude()

Exclude from an array all the elements that match the provided values.

array_exclude(array<string|int, TValue$array, TValue ...$values) : array<string|int, mixed>
Parameters
$array : array<string|int, TValue>

The input array.

$values : TValue

The values to be excluded from array.

Tags
see
array_extract()
  • Extract values from an array
see
array_omit()
  • Omit keys from an array
see
array_pick()
  • Pick keys from an array
Return values
array<string|int, mixed>

Returns a new array containing all the elements in array except for those with values present in values.

array_extract()

Extract from an array all the elements of the input array matching the provided values.

array_extract(array<string|int, mixed> $array, TValue ...$values) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>
$values : TValue

The values to be extracted from array.

Tags
see
array_exclude()
  • Exclude values from an array
see
array_omit()
  • Omit keys from an array
see
array_pick()
  • Pick keys from an array
Return values
array<string|int, mixed>

array_get()

Retrieve an element from an array that can be located several levels deep.

array_get(TArray $array, string ...$keys) : TArray : mixed)

For example:

$data = [ 'foo' => [ 'bar' => [ 'baz' => 'BAZ', 'qux' => ['QUX'] ] ] ];

array_get($data, 'foo', 'bar', 'baz'); // Returns 'BAZ' array_get($data, 'foo', 'bar', 'qux'); // Returns ['QUX'] array_get($data, 'foo', 'bar', 'qux', 0); // Returns 'QUX' array_get($data, 'foo', 'bar'); // Returns $data['foo']['bar'] array_get($data, 'foo'); // Returns $data['foo'] array_get($data); // Returns $data

Parameters
$array : TArray

The array to retrieve the element from.

$keys : string

You can provide a path of keys, where each successive key should be a key on the value of the previous key.

Return values
TArray : mixed)

Returns the value of the desired element, or null if it's not found. If no keys are provided, array is returned.

array_interchange()

Interchange the values of two elements in an array.

array_interchange(array<string|int, mixed> $array, TKey $key1, TKey $key2) : array<string|int, mixed>

If either of the keys doesn't exist in array, then the other key will be set to null, and a PHP Warning will be emitted.

Parameters
$array : array<string|int, mixed>
$key1 : TKey
$key2 : TKey
Return values
array<string|int, mixed>

array_key_types()

Inspects the types of keys used in an array.

array_key_types(array<int|string, mixed> $array) : ($array is array{} ? array{} : ($array is array ? array{integer: true} : ($array is array ? array{string: true} : array{integer: true, string: true})))
Parameters
$array : array<int|string, mixed>

The array to inspect.

Return values
($array is array{} ? array{} : ($array is array ? array{integer: true} : ($array is array ? array{string: true} : array{integer: true, string: true})))

Returns an array whose keys are strings representing the types of keys used in array. The values are always true.

array_omit()

Create a copy of the input array while omiting specific keys.

array_omit(array<string|int, mixed> $array, TKey ...$keys) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>
$keys : TKey

The keys of the elements to be omitted from array.

Tags
see
array_exclude()
  • Exclude values from an array
see
array_extract()
  • Extract values from an array
see
array_pick()
  • Pick keys from an array
Return values
array<string|int, mixed>

Returns a new array containing all the elements in array except for those with keys present in keys.

array_pick()

Create a new array by picking elements from the input array corresponding to a specific set of keys.

array_pick(array<string|int, mixed> $array, TKey ...$keys) : array<string|int, mixed>
Parameters
$array : array<string|int, mixed>
$keys : TKey

The keys of the elements to pick from array.

Tags
see
array_exclude()
  • Exclude values from an array
see
array_extract()
  • Extract values from an array
see
array_omit()
  • Omit keys from an array
Return values
array<string|int, mixed>

array_remap()

Applies a (generator) callback to the elements of a given array, allowing the remapping of its keys in the process.

array_remap(callable(TKey $key, TValue $value): mixed $callback, array<string|int, mixed> $array) : array<string|int, mixed>

array_remap() returns an array containing the results of applying a callback with the corresponding key and value of array used as arguments.

If callback is a generator function, then it's possible to provide a new key for the resulting array element using yield from $key => $value.

If the same key is yielded more than once, then the later yield will override the previous one.

Parameters
$callback : callable(TKey $key, TValue $value): mixed

A callable to run for each key-value pair in the array.

$array : array<string|int, mixed>
Return values
array<string|int, mixed>

Returns an array containing the results of applying the callback function to the corresponding key-value pair of array used as arguments for the callback.

array_split()

Split an array by a value.

array_split(TArray $array, TValue $separator[, int $limit = PHP_INT_MAX ][, bool $strict = true ]) : TArray> : array[]))

Returns a list of arrays, each of which is a subset of array formed by splitting it on boundaries formed by the value separator.

Keys are preserved in the resulting arrays.

Note:

For the ordering of the elements, PHP's array iteration order is used.

Parameters
$array : TArray

The input array.

$separator : TValue

The boundary value.

$limit : int = PHP_INT_MAX

If limit is set and positive, the returned list will contain a maximum of limit arrays with the last array containing the rest of array.

If the limit parameter is negative, all arrays except the last -limit are returned.

If the limit parameter is zero, then this is treated as 1.

$strict : bool = true

By default array_split() will use strict comparisons (===) to match separator against the values of array. If strict is set to false then non-strict comparisons (==) will be used instead.

Return values
TArray> : array[]))

Returns a list of arrays created by splitting the array parameter on boundaries formed by the separator.

If separator contains a value that is not contained in array and a negative limit is used, then an empty list will be returned, otherwise a list containing array will be returned. If separator values appear at the start or end of array, said values will be added as an empty array either in the first or last position of the returned list respectively.

array_zip()

Perform a zip operation on multiple arrays.

array_zip(array<string|int, TValue$array, array<string|int, TValue...$arrays) : array<int, TValue}>
Parameters
$array : array<string|int, TValue>

An array to zip.

$arrays : array<string|int, TValue>

Supplementary list of array arguments to zip.

Return values
array<int, TValue}>

Returns an array whose elements are each an array holding the elements of the input arrays at the same index.


        
On this page

Search results