How to use Array Helpers in Laravel

Laravel is an amazing framework that provides multiple tools, features, and many helper functions. Array Helpers in Laravel aid developers to work with arrays of data. These helper functions greatly save coding time and make working with arrays more manageable. Lets go through different Laravel Helper functions. 

Array Helpers in Laravel

join()

The join() function works on an array like the implode() function. 

use Illuminate\Support\Arr;

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

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

// Tailwind, Alpine, Laravel, Livewire

implode($stack, ', ');

// Tailwind, Alpine, Laravel, Livewire

As shown above, both functions work the same way. However, the join() helper function is more helpful if we want to add a separate string joining to the last value of the array.

For example, use the join function and add an extra string, as shown below, to get the output as stated.

use Illuminate\Support\Arr;

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

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

// Tailwind, Alpine, Laravel, and Livewire

keyBy()

An array of various products or data can be given an attribute to key the data. This helps target the data based on the assigned key value. 

The keyBy() function eases this process of targeting data based on a key value. It cuts down the longer code as shown below into a single line of code.

$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'],

   ]

*/

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

The example above illustrates how the keyBy() helper function aids in writing concise and short codes.

get()

The get() helper function helps read specific data from an array.

use Illuminate\Support\Arr;

$data = [

   'products' => [

       'desk' => [

           'name' => 'Oakendesk'

           'price' => 599.00,

           'description' => 'Solid oak desk built from scratch.'

       ],

   ],

];

Arr::get($data, 'products.desk.price'); 

// 599.00

Arr::get($data, 'products.desk.discount');

// Returns null

Arr::get($data, 'products.desk.discount', ['type' => 'percent', 'value' => 10]);

// Returns custom default value if not found.

The get() function returns specific data from an array in a single line function, as shown above. 

first() and last()

As their name, the first() and last() helper functions return the initial or ending value from an array. 

$array = [100, 200, 300, 110];

end($array);

// 110

The end() function will return false, if an array is empty.

The last() function can also be used to retrieve the last value of an array. It provides varying outputs based on the input values. 

Arr::last($array, null, 100); // 100

This way last() will output a default value of 100, even if the array is empty.

We can also add closures along with the first() and last() functions to apply any condition on the returning value. These allow you to add specific filters to the returning value. 

$array = [100, 200, 300, 110];

Arr::last($array, fn ($e) => $e > 110); // 300

Arr::first($array, fn ($e) => $e > 110); // 200

pluck()

The pluck() helper function aids to fetch specific data fields from a multi-value array.

$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",

]

*/

The above complex code can be simplified by using the pluck() function on the array, as shown below. 

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

Hope the helper functions explained above greatly ease and simplify your complex code structure. There are many more helpers that can be used to process data from arrays in Laravel. Find a complete list of documentation of Helpers in Laravel.