The elegance, simplicity, and expressiveness of Laravel are well known. Laravel's helper functions are among its most powerful features. The array helpers in Laravel are a crucial tool for developers working with data arrays.


We'll look at a few Laravel array helper functions in this article that every developer should be familiar with. Working with arrays can be simplified and time-saving thanks to these aids. We'll discuss the functions join(), keyBy(), get(), first(), last(), and pluck (). Let's explore the array helpers provided by Laravel now.


Array Join


This works the same way as implode() so it depends on your which function you prefer.


use Illuminate\Support\Arr;

 

$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];

 

Arr::join($stack, ', ');

// Tailwind, Alpine, Laravel, Livewire

 

implode($stack, ', ');

// Tailwind, Alpine, Laravel, Livewire


join() also comes with the feature last value to use a separate joining string.


use Illuminate\Support\Arr;

 

$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];

 

Arr::join($stack, ', ', ', and');

// Tailwind, Alpine, Laravel, and Livewire


Keyed Array data


This will create a new variable by stuffing the data into it by keyed value.


$array = [
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
];
 
$keyed = [];
 
foreach ($array as $value) {
    $keyed[$value['product_id']] = $value;
}
 
/*
    [
        'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
        'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]
*/

Using the Arr::keyBy() method, you can do the same thing with one line of code:

$keyed = Arr::keyBy($array, 'product_id');

Checking and Getting Data from an Array

The mighty Arr::get() method is simple to use, but contains a powerful "dot notation" you can use to get nested data easily same goes with Arr::has():

use Illuminate\Support\Arr;
 
$data = [
    'products' => [
        'desk' => [
            'name' => 'Oakendesk'
            'price' => 599.00,
            'description' => 'Solid oak desk built from scratch.'
        ],
    ],
];
 
// 599.00
Arr::get($data, 'products.desk.price');
 
// Returns false
Arr::has($data, 'products.desk.discount');
 
// Returns null
Arr::get($data, 'products.desk.discount');
 
// Returns custom default value if not found.
Arr::get($data, 'products.desk.discount', ['type' => 'percent', 'value' => 10]);


Getting the first or last element in an array


When you have an array and want to get the last element, you can reach for the end() function in PHP:

$array = [100, 200, 300, 110];
 
end($array);

If your array is empty, though, you will get false instead:

$array = [];
end($array); // false

Using Laravel's last() helper, you have multiple options when an array is empty:

use Illuminate\Support\Arr;
 
$array = [];
 
Arr::last($array); // null
 
// Provide a default
Arr::last($array, null, 100); // 100


Using Laravel's helper also enables you to pass a closure as a second argument as the condition for which element to return first or last respectively:

$array = [100, 200, 300, 110];
 
Arr::last($array, fn ($e) => $e > 110); // 300
Arr::first($array, fn ($e) => $e > 110); // 200

Simple but powerful API for the many situations you might find yourself in with getting the first or last element within array data.


Plucking Data from an Array


To get one scalar piece of data from a collection of data (i.e., emails from users):

$array = [
    ['user' => ['id' => 1, 'name' => 'User 1', 'email' => 'user1@example.com']],
    ['user' => ['id' => 2, 'name' => 'User 2', 'email' => 'user2@example.com']],
];
 
$emails = [];
 
foreach ($array as $result) {
    $emails[] = $result['user']['email'];
}
 
/*
[
    "user1@example.com",
    "user2@example.com",
]
*/

Laravel's Arr::pluck() helper makes this trivial:

Arr::pluck($array, 'user.email');


These are some of the most used and helpful helpers in Laravel. Let me know in the comments if you know more helpers.