I found a very good article when browsing the laravel news blog that talks about Laravel Array Helpers, which every developer should know. I know about them, but it's great to share them with you because I know the importance of this Array
- Array Join
use Illuminate\Support\Arr;
$stack = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
Arr::join($stack, ', ');
// Tailwind, Alpine, Laravel, Livewire
implode($stack, ', ');
// Tailwind, Alpine, Laravel, Livewire
$stack_2 = ['Tailwind', 'Alpine', 'Laravel', 'Livewire'];
Arr::join($stack_2, ', ', ', and');
// Tailwind, Alpine, Laravel, and Livewire
- Keyed Array data
$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
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
// end()
$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
- Plucking Data from an 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",
]
*/
// Laravel's Arr::pluck() helper makes this trivial
Arr::pluck($array, 'user.email');
I gave you, in short, what was mentioned in the platform, but you must visit the article
- https://laravel-news.com/laravel-array-helpers-every-developer-should-know-about
- https://laravel.com/docs/10.x/collections
- https://laravel.com/docs/10.x/helpers
and go deeper and deeper to take full advantage.
Top comments (0)