Array helper functions in Laravel

One of the most powerful features of Laravel is its helper functions or facades. Working in real-life projects storing data in an array is a common approach. Laravel’s array helpers provide more functions than traditional PHP.

In this article, we will see the most commonly used array helper functions in Laravel. This helper function saves development time and we don’t need to apply custom logic for some specific tasks. Here, we will cover exists(), join(), get(), first(), last(), random(), and pluck().

Checking Key Exists Or Not

The array exists function checks given key exists in an array or not. it will return a boolean as output. This function is used when before performing business logic we need to check whether specific data exists or not in the array. Let’s take a simple example:

use Illuminate\Support\Arr;
$array = ['name' => 'Code Wolfy', 'status' => 'Active'];
$exists = Arr::exists($array, 'name'); // true
$exists = Arr::exists($array, 'url'); // false

Here, First of we need to include a namespace for array facade. Then we have defined an array with name and status as keys. At first, we will check array contains name as a key or not. It will return true.

When we check URL exists or not then it will return false.

Array Join Using String

The array join combines elements of an array into a string. It’s an alternative to implode function. However, it can add a custom join string for the last element of the array. Let’s take an example to understand the difference:

use Illuminate\Support\Arr;
$array = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
Arr::join($array, ', '); 
// Tailwind, Alpine, Laravel, Livewire

implode($array, ', '); 
// Tailwind, Alpine, Laravel, Livewire
Arr::join($array, ', ', ' and ');
// Tailwind, Alpine, Laravel, and Livewire

As you can see, the first array join() and implode() will return the same output. However the second time it adds the and keyword for the last element.

Getting Data from an Array

With the help of the Arr::get() method we can simply get nested data using “dot notation” or even set the default value if a key isn’t found.

use Illuminate\Support\Arr;
$data = [
    'books' => [
        'name' => 'Laravel For Dummies'
        'price' => 199.00,
        'description' => 'Built Laravel application from scratch.'
Arr::get($data, 'books.price'); // 199.0

Arr::has($data, ''); // false

Arr::get($data, ''); // null

// Returns custom default value
Arr::get($data, '', "Tylor Otwell");

In above example, we have created a simple array and used get() and has() methods to access data using dot notation.

Plucking Data from an Array

The Arr::pluck method retrieves all of the values for a given key from an array. For example, we have an array of users that contain the user name and a unique ID. If we want to retrieve only names then we can use the pluck() method.

use Illuminate\Support\Arr;

$array = [
    ['user' => ['id' => 1, 'name' => 'User 1']],
    ['user' => ['id' => 2, 'name' => 'User 2']],
    ['user' => ['id' => 3, 'name' => 'User 3']],
    ['user' => ['id' => 4, 'name' => 'User 4']],

$names = Arr::pluck($array, '');
// ["User 1", "User 2", "User 3", "User 4"]

However, we can pluck 2 keys and set one as key and other as a value like the below example:

use Illuminate\Support\Arr;

$array = [
    ['user' => ['id' => 1, 'name' => 'User 1']],
    ['user' => ['id' => 2, 'name' => 'User 2']],
    ['user' => ['id' => 3, 'name' => 'User 3']],
    ['user' => ['id' => 4, 'name' => 'User 4']],

$names = Arr::pluck($array, '', '');
// [1 => "User 1", 2 => "User 2", 3 =>  "User 3", 4 => "User 4"]

Getting Random Element From Array

The array random() method returns one or more random values from an array.

use Illuminate\Support\Arr;
$array = [1, 2, 3, 4, 5];

$random = Arr::random($array); //5
$random = Arr::random($array, 2); // [1, 4]

Getting First Or Last Element From Array

For example, you want to get the first or last element that passes specific conditions or get default value if an array is empty.

use Illuminate\Support\Arr;
$array = [100, 200, 300];

Arr::first($array); // 100
Arr::last($array); // 200 

Arr::first($array, function (int $value, int $key) {
    return $value >= 200;
// 200

In this example, we have reviewed some of the array helpers in Laravel. There are many other array helper functions in Laravel that can be used in various situations. These are the most commonly used functions on a regular basis.