Use Arr::get to Get Value From Deeply Nested Array in Laravel

Hello Artisan,

In this example, I will show you a very easy way to retrieve a value from a deeply nested array using "dot" notation in Laravel. Using Arr::get, we can fetch array values with dot notation. It doesn't matter how many nested arrays you are gonna fetch.

I will just show you a simple example with source code so that you can understand. Before releasing the Laravel 6 version, we can use the like array_get() method. But after releasing the version Laravel six, we can use it as the Arr::get function. 

See the example code:

Route::get('test',function(){
    $arr = [
        'institution' => [
            'type' => [
                'school' => [
                    'name' => 'Islami Academy High School'
                ]
            ]
        ]
    ];
    return \Arr::get($arr, 'institution.type.school.name', 'default_value');
});

 

Now if you visit the http://127.0.0.1:8000/test URL, then you will see that output:

 

output
Islami Academy High School

 

Read also: How to Implement Rate Limiting in Laravel 8

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js
ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT