class Arr (View source)

Array functions. All of these methods accept Traversable or ArrayAccess objects in addition to arrays.

Most of these methods are also provided in Bag, but their implementation is significant enough that the backing logic lives here so they can be used standalone from Bags.

Methods

static array
from(iterable|null|stdClass $iterable)

Converts an iterable, null, or stdClass to an array.

static array
fromRecursive(iterable|null|stdClass $iterable)

Recursively converts an iterable to nested arrays.

static array
column(iterable $input, string|int|null $columnKey, string|int|null $indexKey = null)

Return the values from a single column in the $input array, identified by the $columnKey.

static bool
has(array|ArrayAccess $data, string $path)

Returns whether a key exists from an array or ArrayAccess object using path syntax to check nested data.

static mixed|null
get(array|ArrayAccess $data, string $path, mixed|null $default = null)

Gets a value from an array or ArrayAccess object using path syntax to retrieve nested data.

static 
set(array|ArrayAccess $data, string $path, mixed $value)

Sets a value in a nested array or ArrayAccess object using path syntax to set nested data.

static mixed
remove(array|ArrayAccess $data, string $path, mixed|null $default = null)

Removes and returns a value from an array or ArrayAccess object using path syntax to remove nested data.

static bool
isAccessible(mixed $value)

Returns whether the value is an array or an object implementing ArrayAccess.

static 
assertAccessible(mixed $value) deprecated

Asserts that the given value is an array or an object implementing ArrayAccess.

static bool
isAssociative(iterable $iterable)

Returns whether the $iterable is an associative mapping.

static bool
isIndexed(iterable $iterable)

Returns whether the $iterable is an indexed list - zero indexed and sequential.

static array
mapRecursive(iterable $iterable, callable $callable)

Returns an array with the $callable applied to each leaf value in the $iterable.

static array
replaceRecursive(iterable $iterable1, iterable $iterable2)

Replaces values from second iterable into first iterable recursively.

static array
flatten(iterable $iterable, int $depth = 1)

Flattens an iterable.

Details

static array from(iterable|null|stdClass $iterable)

Converts an iterable, null, or stdClass to an array.

Parameters

iterable|null|stdClass $iterable

Return Value

array

static array fromRecursive(iterable|null|stdClass $iterable)

Recursively converts an iterable to nested arrays.

Parameters

iterable|null|stdClass $iterable

Return Value

array

static array column(iterable $input, string|int|null $columnKey, string|int|null $indexKey = null)

Return the values from a single column in the $input array, identified by the $columnKey.

Optionally, an $indexKey may be provided to index the values in the returned array by the values from the $indexKey column in the input array.

Example:

$data = [
    ['id' => 10, 'name' => 'Alice'],
    ['id' => 20, 'name' => 'Bob'],
    ['id' => 30, 'name' => 'Carson'],
];

Arr::column($data, 'name');
// => ['Alice', 'Bob', 'Carson']

Arr::column($data, 'name', 'id');
// => [10 => 'Alice', 20 => 'Bob', 30 => 'Carson']


Note: This matches the array_column function, however; it accepts an iterable not just an array, it allows for mapping a list of objects implementing ArrayAccess, and allows for mapping a list of object properties (which was added to the builtin function in PHP 7.0).

Parameters

iterable $input A list of arrays or objects from which to pull a column of values
string|int|null $columnKey The key of the values to return or null for no change
string|int|null $indexKey The key of the keys to return or null for no change

Return Value

array

static bool has(array|ArrayAccess $data, string $path)

Returns whether a key exists from an array or ArrayAccess object using path syntax to check nested data.

This method does not allow for keys that contain /.

Example:

// Check if the the bar key of a set of nested arrays exists.
// This is equivalent to isset($data['foo']['baz']['bar']) but won't
// throw warnings for missing keys.
Arr::has($data, 'foo/baz/bar');


Note: Using isset() with nested data, like isset($data['a']['b']), won't call offsetExists for 'a'. It calls offsetGet('a') and if 'a' doesn't exist and an isset check isn't done in offsetGet, a warning is triggered. It could be argued that that ArrayAccess object should fix this in their implementation of offsetGet, and I would agree. Regardless I think this is nicer syntax.

Parameters

array|ArrayAccess $data Data to check values from
string $path Path to traverse and check keys from

Return Value

bool

static mixed|null get(array|ArrayAccess $data, string $path, mixed|null $default = null)

Gets a value from an array or ArrayAccess object using path syntax to retrieve nested data.

Example:

// Get the bar key of a set of nested arrays.
// This is equivalent to $data['foo']['baz']['bar'] but won't
// throw warnings for missing keys.
Arr::get($data, 'foo/baz/bar');

This method does not allow for keys that contain /.

This code is adapted from Michael Dowling in his Guzzle library.

Parameters

array|ArrayAccess $data Data to retrieve values from
string $path Path to traverse and retrieve a value from
mixed|null $default Default value to return if key does not exist

Return Value

mixed|null

static set(array|ArrayAccess $data, string $path, mixed $value)

Sets a value in a nested array or ArrayAccess object using path syntax to set nested data.

Inner arrays will be created as needed to set the value.

Example:

// Set an item at a nested structure
Arr::set($data, 'nested/path/hello', 'world');

// Append to a list in a nested structure
Arr::get($data, 'foo/baz');
// => null
Arr::set($data, 'foo/baz/[]', 'a');
Arr::set($data, 'foo/baz/[]', 'b');
Arr::get($data, 'foo/baz');
// => ['a', 'b']

This function does not support keys that contain / or [] characters because these are special tokens used when traversing the data structure. A value may be appended to an existing array by using [] as the final key of a path.


Note: To set values in arrays that are in ArrayAccess objects their offsetGet() method needs to be able to return arrays by reference. See MutableBag for an example of this.

This code is adapted from Michael Dowling in his Guzzle library.

Parameters

array|ArrayAccess $data Data to modify by reference
string $path Path to set
mixed $value Value to set at the key

Exceptions

RuntimeException when trying to set a path that travels through a scalar value
RuntimeException when trying to set a value in an array that is in an ArrayAccess object which cannot retrieve arrays by reference

static mixed remove(array|ArrayAccess $data, string $path, mixed|null $default = null)

Removes and returns a value from an array or ArrayAccess object using path syntax to remove nested data.

Example:

Arr::remove($data, 'foo/bar');
// => 'baz'
Arr::remove($data, 'foo/bar');
// => null

This function does not support keys that contain /.


Note: To remove values in arrays that are in ArrayAccess objects their offsetGet() method needs to be able to return arrays by reference. See MutableBag for an example of this.

Parameters

array|ArrayAccess $data Data to retrieve remove value from
string $path Path to traverse
mixed|null $default Default value to return if key does not exist

Return Value

mixed

Exceptions

RuntimeException when trying to remove a path that travels through a scalar value
RuntimeException when trying to remove a value in an array that is in an ArrayAccess object which cannot retrieve arrays by reference

static bool isAccessible(mixed $value)

Returns whether the value is an array or an object implementing ArrayAccess.

Parameters

mixed $value

Return Value

bool

static assertAccessible(mixed $value) deprecated

deprecated since 1.0 and will be removed in 2.0. Use \Bolt\Common\Assert::isArrayAccessible instead.

Asserts that the given value is an array or an object implementing ArrayAccess.

Parameters

mixed $value

Exceptions

InvalidArgumentException when it is not

static bool isAssociative(iterable $iterable)

Returns whether the $iterable is an associative mapping.

Note: Empty arrays are not.

Parameters

iterable $iterable

Return Value

bool

static bool isIndexed(iterable $iterable)

Returns whether the $iterable is an indexed list - zero indexed and sequential.

Note: Empty iterables are.

Parameters

iterable $iterable

Return Value

bool

static array mapRecursive(iterable $iterable, callable $callable)

Returns an array with the $callable applied to each leaf value in the $iterable.

This converts all Traversable objects to arrays.

Parameters

iterable $iterable
callable $callable Function is passed ($value, $key)

Return Value

array

static array replaceRecursive(iterable $iterable1, iterable $iterable2)

Replaces values from second iterable into first iterable recursively.

This differs from array_replace_recursive in a couple ways:

  • Lists (indexed arrays) from second array completely replace list in first array.

  • Null values from second array do not replace lists or associative arrays in first (they do still replace scalar values).

This converts all Traversable objects to arrays.

Parameters

iterable $iterable1
iterable $iterable2

Return Value

array The combined array

static array flatten(iterable $iterable, int $depth = 1)

Flattens an iterable.

Example:

// Flatten one level
Arr::flatten([[1, 2], [[3]], 4])
// => [1, 2, [3], 4]

// Flatten all levels
Arr::flatten([[1, 2], [[3]], 4], INF)
// => [1, 2, 3, 4]

Parameters

iterable $iterable The iterable to flatten
int $depth How deep to flatten

Return Value

array