Arrays are the bread and butter of any web application that deals with data. PHP already offers nearly 80 functions to perform various operations on arrays, and Laravel complements them with a handful of practical functions that are inspired by certain functions found in Python and Ruby.
Note
Several of Laravel's classes, including Eloquent collections, implement the PHP ArrayAccess
interface. This means that you can use them like a normal array in your code and, for instance, iterate over the items in a foreach
loop or use them with the array functions described here.
Most of the functions support a dot notation to refer to nested values, which is similar to JavaScript objects. For example, rather than writing $arr['foo']['bar']['baz']
, you can use the array_get
helper and write array_get($arr, 'foo.bar.baz');
.
In the following usage examples, we will use three dummy arrays and assume that they are reset for each example:
$associative = [ 'foo' => 1, 'bar' => 2, ]; $multidimensional = [ 'foo' => [ 'bar' => 123, ], ]; $list_key_values = [ ['foo' => 'bar'], ['foo' => 'baz'], ];
We will now take a look at how we can use Laravel's array helper functions to extract and manipulate the values of those arrays:
To retrieve a value with a fallback value if the key does not exist, we use the
array_get
function as follows:array_get($multidimensional, 'foo.bar', 'default'); // Returns 123
This is helpful if you are referencing an array key that may or may not exist (that is, in an array of request data). If the key does not exist, then the default value will be returned instead.
To remove a value from an array using the dot notation, we use the
array_forget
function as follows:array_forget($multidimensional, 'foo.bar'); // $multidimensional == ['foo' => []];
To remove a value from an array and return it, we use the
array_pull
function as follows:array_pull($multidimensional, 'foo.bar'); // Returns 123 and removes the value from the array
To set a nested value using the dot notation, we use the
array_set
function as follows:array_set($multidimensional, 'foo.baz', '456'); // $multidimensional == ['foo' => ['bar' => 123, 'baz' => '456']];
To flatten a multidimensional associative array, we use the
array_dot
function as follows:array_dot($multidimensional); // Returns ['foo.bar' => 123]; array_dot($list_key_values); // Returns ['0.foo' => 'bar', '1.foo' => 'baz'];
To return all of the keys and their values from the array except for the ones that are specified, we use the
array_except
function as follows:array_except($associative, ['foo']); // Returns ['bar' => 2];
To only extract some keys from an array, we use the
array_only
function as follows:array_only($associative, ['bar']); // Returns ['bar' => 2];
To return a flattened array containing all of the nested values (the keys are dropped), we use the
array_fetch
function as follows:array_fetch($list_key_values, 'foo'); // Returns ['bar', 'baz'];
To iterate over the array and return the first value for which the closure returns true, we use the
array_first
function as follows:array_first($associative, function($key, $value) { return $key == 'foo'; }); // Returns 1
To generate a one-dimensional array containing only the values that are found in a multidimensional array, we use the
array_flatten
function as follows:array_flatten($multidimensional); // Returns [123]
To extract an array of values from a list of key-value pairs, we use the
array_pluck
function as follows:array_pluck($list_key_values, 'foo'); // Returns ['bar', 'baz'];
To get the first or last item of an array (this also works with the values returned by functions), we use the
head
andlast
functions as follows:head($array); // Aliases to reset($array) last($array); // Aliases to end($array)