Skip to content
the poor coder

Arr::get() The Arr::get method retrieves a value from a deeply nested array using "dot" notation:

Poor Man
Published
Reading time
2 min read
On this page

Understanding the Arr::get() Method#

If you are working with arrays in PHP, you might have come across situations where you need to retrieve a value from a deeply nested array. This can be a tedious task as you have to traverse through the array using multiple foreach loops. Luckily, Laravel provides a convenient method called Arr::get() that makes this task much easier.

What is the Arr::get() Method?#

The Arr::get() method is a static method in Laravel's Illuminate\Support\Arr class that allows you to retrieve a value from a deeply nested array using "dot" notation.

How Does the Arr::get() Method Work?#

The syntax for using the Arr::get() method is as follows:

Text
Arr::get(array $array, string $key, mixed $default = null): mixed

The $array parameter is the array from which you want to retrieve the value. The $key parameter is the key of the value you want to retrieve. The $default parameter is the default value that will be returned if the key does not exist in the array.

The key parameter can be a string or an array of strings. If it is an array of strings, the method will traverse through the array using each key until it finds the final value.

Here is an example:

Text
$array = [
    'user' => [
        'name' => 'John Doe',
        'email' => 'john.doe@example.com',
        'phone' => [
            'home' => '123-456-7890',
            'work' => '987-654-3210'
        ]
    ]
];

// Retrieve the value of the 'work' phone number
$workPhone = Arr::get($array, 'user.phone.work');

echo $workPhone; // Outputs: 987-654-3210

In this example, we have a nested array that contains a user's name, email, and phone numbers. We want to retrieve the value of the "work" phone number. We do this by passing the array and the key "user.phone.work" to the Arr::get() method.

Multiple Ways to Use the Arr::get() Method#

There are multiple ways to use the Arr::get() method depending on your needs:

  • You can pass a default value as the third parameter. This value will be returned if the key does not exist in the array. For example:
Text
// Retrieve the value of the 'fax' phone number or return 'N/A' if it doesn't exist
$fax = Arr::get($array, 'user.phone.fax', 'N/A');
  • You can use the ?? operator to provide a default value inline like so:
Text
// Retrieve the value of the 'fax' phone number or return 'N/A' if it doesn't exist
$fax = Arr::get($array, 'user.phone.fax') ?? 'N/A';

This is equivalent to passing 'N/A' as the third parameter.

The Arr::get() method is a powerful tool for working with arrays in PHP. It can save you a lot of time and effort when dealing with nested arrays.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC