Laravel Arr get() function Example

In this example, I explain how to use laravel Arr get() function. Laravel arr get() function to you can get array element value. The Arr::get method retrieves a value from a deeply nested array using “dot” notation.

Example : Laravel array get() function

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Arr;

class UserController extends Controller
{
    public function index(){        
        $array = ['products' => ['desk' => ['price' => 445]]];

        $price = Arr::get($array, 'products.desk.price');

        print_r($price);
        //445

        $array1 = ['car' => ['bmw' => ['no' =>'ks-999',],'honda' => ['no' =>'cs-888',]]];

        $price1 = Arr::get($array, 'car.bmw.no');

        print_r($price1);
        //ks-999
    }
}

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::get('users', [UserController::class,'index']);

I hope you understand laravel arr get function and it can help you…