Laravel provides many helper functions for interacting and manipulating array data structures. Laravel's array functions offer additional functionality on top of PHP's built in array functions. The helper functions in this section are located within the "Illuminate\Support\Arr" static class. There are methods defined in the static class that have counterparts defined in the global namespace. These functions appear below each static array method.
#Immutable Arrays
All of the array helper functions below, unless stated otherwise, treat arrays as an immutable data structure. This means that any changes made to an array are returned as a copy of the original array, with the original array remaining unchanged.
Here is a quick example:
1use Illuminate\Support\Arr;
2
3$originalArray = [
4 'fruit' => 'orange',
5 'vegetable' => 'carrot'
6];
7
8$anotherArray = array_add($originalArray, 'vehicle', 'car');
Executing var_dump
on $originalArray
would produce the following output:
1array {
2 ["fruit"] "orange"
3 ["vegetable"] "carrot"
4}
This is because Laravel's functions do not affect the original array (unless stated otherwise).
Running var_dump
on $anotherArray
, however, would produce the expected results:
1array {
2 ["fruit"] "orange"
3 ["vegetable"] "carrot"
4 ["vehicle"] "car"
5}
#Array Dot Notation
In addition to treating arrays as immutable, Laravel's functions support "dot" notation when accessing items in an array. This is similar to JavaScript's dot notation when accessing object properties.
JavaScript:
1forms = document.forms;
PHP, using Laravel's helper functions:
1use Illuminate\Support\Arr;
2
3// Create a multi-dimensional array, to simulate a document and forms.
4$testDocument = [
5 'document' => [
6 'forms' => [
7
8 ]
9 ]
10];
11
12// Get the forms.
13$forms = Arr::get($testDocument, 'document.forms');
14
15// Traditionally, with vanilla PHP:
16$forms = $testDocument['document']['forms'];
Using dot notation with Laravel's functions will be explored in more detail in the following articles.
- Laravel 5: Adding Elements to the Array with add
- Laravel 5: Splitting an Array Into its Keys and Values with divide
- Laravel 5: Collapsing a Multi-Dimensional Array to a Single Level With collapse
- Laravel 5: Representing Multi-Dimensional Arrays in Dot Notation With dot
- Laravel 5: Excluding Items From an Array With except
- Laravel 5: Filtering Array Elements With only
- Laravel 5: Reducing a Multi-Dimensional Array to a Single Dimension Losing Keys With flatten
- Laravel 5: Finding the First Occurrence of an Element Matching a Condition With first
- Laravel 5: Finding the Last Occurrence of an Element Matching a Condition With last
- Laravel 5: Checking if an Array Contains an Element With has
- Laravel 5: Adding or Setting New Array Element Values With set
- Laravel 5: Removing Elements From an Array With forget
- Laravel 5: Retrieving Elements from an Array With get
- Laravel 5: Retrieving, and Removing an Element From an Array With pull
- Laravel 5: Retrieving Nested Array Values With pluck
- Laravel 5: Conditionally Retrieving Array Values With where
- Laravel 5: Sorting Arrays With sort
- Laravel 5: Checking if an Array is an Associative Array With isAssoc
- Laravel 5: Determining if an Object is Array Accessible With accessible
- Laravel 5: Determining if an Array Contains an Element With exists
- Laravel 5: Adding a New Element to the Beginning of an Array With prepend
- Laravel 5: Creating Combinations of Elements With crossJoin
- Laravel 5: Ensuring a Value is an Array With wrap
- Laravel 5: Randomizing Element Order With shuffle
- Laravel 5: Retrieving Random Elements from an Array With random
- Laravel 5: Getting the First Element of an Array With head
- Laravel 5: Getting the Last Element of an Array With last
∎