Member-only story

Exploring Laravel Array Helpers: A Comprehensive Guide

ArjunAmrutiya
3 min readAug 31, 2023

--

Introduction
When working with arrays in Laravel, you’ll often find yourself needing to perform various operations such as filtering, sorting, transforming, and more. Laravel provides a set of incredibly useful array helper functions that streamline these operations and make array manipulation a breeze. In this blog post, we’ll dive deep into these Laravel array helpers with detailed code examples to demonstrate their usage.

1. array_add

The array_add function allows you to add a key-value pair to an array if the specified key does not already exist. If the key already exists, the array remains unchanged.

$originalArray = ['name' => 'John'];
$newArray = array_add($originalArray, 'age', 30);

// Output: ['name' => 'John', 'age' => 30]

2. array_get

With array_get, you can access an array element using "dot" notation for nested arrays. If the specified key doesn't exist, you can provide a default value that will be returned.

$data = ['user' => ['name' => 'Jane', 'age' => 25]];
$name = array_get($data, 'user.name', 'Unknown');

// Output: 'Jane'

3. array_except

--

--

ArjunAmrutiya
ArjunAmrutiya

Written by ArjunAmrutiya

👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!

Responses (1)