Arr
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
Return the values from a single column in the $input
array, identified by the $columnKey
.
Returns whether a key exists from an array or ArrayAccess
object using path syntax to check nested data.
Gets a value from an array or ArrayAccess
object using path syntax to retrieve nested data.
Sets a value in a nested array or ArrayAccess
object using path syntax to set nested data.
Removes and returns a value from an array or ArrayAccess
object using path syntax to remove nested data.
Returns whether the value is an array or an object implementing ArrayAccess
.
Asserts that the given value is an array or an object implementing ArrayAccess
.
Returns whether the $iterable
is an associative mapping.
Returns whether the $iterable
is an indexed list - zero indexed and sequential.
Returns an array with the $callable
applied to each leaf value in the $iterable
.
Replaces values from second iterable into first iterable recursively.
Flattens an iterable.
Details
at line 33
static array
from(iterable|null|stdClass $iterable)
Converts an iterable
, null
, or stdClass
to an array.
at line 62
static array
fromRecursive(iterable|null|stdClass $iterable)
Recursively converts an iterable
to nested arrays.
at line 107
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).
at line 180
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.
at line 220
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.
at line 274
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.
at line 380
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.
at line 411
static bool
isAccessible(mixed $value)
Returns whether the value is an array or an object implementing ArrayAccess
.
at line 425
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
.
at line 441
static bool
isAssociative(iterable $iterable)
Returns whether the $iterable
is an associative mapping.
Note: Empty arrays are not.
at line 462
static bool
isIndexed(iterable $iterable)
Returns whether the $iterable
is an indexed list - zero indexed and sequential.
Note: Empty iterables are.
at line 481
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.
at line 533
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.
at line 674
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]