DEV Community

Morcos Gad
Morcos Gad

Posted on

3 1 1 1 1

Laravel Array Helpers

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
Enter fullscreen mode Exit fullscreen mode
  • 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');

Enter fullscreen mode Exit fullscreen mode
  • 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]);

Enter fullscreen mode Exit fullscreen mode
  • 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

Enter fullscreen mode Exit fullscreen mode
  • 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');

Enter fullscreen mode Exit fullscreen mode

I gave you, in short, what was mentioned in the platform, but you must visit the article

and go deeper and deeper to take full advantage.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay