Author - Navneet Kaur Post Views - 46 views
Laravel Array

Laravel Array Helpers

Elegant, straightforward, and expressive are all characteristics of Laravel. Laravel’s helper functions are among its most effective features. The array helpers in Laravel are a crucial tool for developers dealing with data arrays.
The helper methods for Laravel arrays that every developer should be familiar with are covered in this post. Working with arrays is made easier and more efficient by these helpers. We’ll go through join(), keyBy(), get(), first(), last(), and pluck ().

Array Join

You may be asking yourself, “Why do I need this helper when I can just use join() or implode()?”
use Illuminate\Support\Arr;
$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
Arr::join($stack, ', ');
// Tailwind, Alpine, Laravel, Livewire
implode($stack, ', ');
// Tailwind, Alpine, Laravel, Livewire

The above method is exactly the same, so it’s up to you to decide which method you prefer. When you wish the last value to use a different joining string, the join() helper is beneficial:
use Illuminate\Support\Arr;
$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
Arr::join($stack, ', ', ', and');
// Tailwind, Alpine, Laravel, and Livewire

Keyed Array data
You may take an array of data (for example, many products) and key the data by a certain product attribute. This allows you to easily focus data on a certain key. You could have typed something like this, establishing a new variable and inserting data into it using a 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'],
] */

The following can be done with just one line of code by using the Arr::keyBy() method:
$keyed = Arr::keyBy($array, 'product_id');

Checking and Getting Data from an Array

The Arr::get() function is straightforward to use, but it has a strong “dot notation” that you may use to easily access nested data:
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 wish to obtain the last element, use PHP’s end() function:
$array = [100, 200, 300, 110];
end($array);

If your array is empty, though, you will obtain false:
$array = [];
end($array); // false

When an array is empty, you have many alternatives using Laravel’s last() helper:
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

Straightforward yet powerful API for accessing the first or final member in an array of data.

Data Extraction from an Array
Occasionally you need to extract one scalar item of data from a collection of data (for example, 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 useful:
Arr::pluck($array, 'user.email');

If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri