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 = array( "foo" => 1, "bar" => 2, ); $multidimensional = array( "foo" => array( "bar" => 123, ), ); $list_key_values = array( array("foo" => "bar"), array("foo" => "baz"), );
We will now have 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:array_get($multidimensional, 'foo.bar', 'default'); // Returns 123
To remove a value from an array using the dot notation, we use the
array_forget
function:array_forget($multidimensional, 'foo.bar'); // $multidimensional == array( 'foo' => array() );
To remove a value from an array and return it, we use the
array_pull
function: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:array_set($multidimensional, 'foo.baz', '456'); // $multidimensional == array( 'foo' => array( 'bar' => 123, 'baz' => '456' ) );
To flatten a multidimensional associative array, we use the
array_dot
function:array_dot($multidimensional); // Returns array( 'foo.bar' => 123 ); array_dot($list_key_values); // Returns array( '0.foo' => 'bar', '1.foo' => 'baz' );
To return all of the the keys and their values from the array except for the ones that are specified, we use the
array_except
function:array_except($associative, array('foo',)); // Returns array('bar' => 2);
To only extract some keys from an array, we use the
array_only
function:array_only($associative, array('bar')); // Returns array('bar' => 2);
To return a flattened array containing all of the the nested values (the keys are dropped), we use the
array_fetch
function:array_fetch($list_key_values, 'foo'); // Returns array('bar', 'baz')
To iterate over the array and return the first value for which the closure returns true, we use the
array_first
function: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:array_flatten($multidimensional); // Returns array(123)
To extract an array of values from a list of key-value pairs, we use the
array_pluck
function:array_pluck($list_key_values, 'foo'); // Returns array('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:head($array); // Aliases to reset($array) last($array); // Aliases to end($array)