Laravel Arr flatten() function Example

In this post, I explain how to use laravel Arr flatten() function. Laravel Arr flatten() function is use to convert multidimensional array to single array.

We need to import Illuminate\Support\Arr namespace with use keyword. When you use the Arr flatten()function in your laravel application.

Arr flatten() is one of the laravel helper function. Also laravel provide many helpers functions, this function you can use in framework itself.

Read Also :  How to get Random Value from Array in laravel

  Example of Laravel Arr::flatten() 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(){        
        $array1 = ['name' => 'Web-tuts', 'languages' => ['Laravel', 'PHP','jQuery']];

        $flattened1 = Arr::flatten($array1);
        print_r($flattened1);

        echo "<br>";
        $array2 = ['car' => 'BMW', 'bike' => ['ktm', 'bajaj']];

        $flattened2 = Arr::flatten($array2);
        print_r($flattened2);
        
    }
}

routes/web.php

<?php

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

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

I hope you understan of laravel Arr::flatten() function and it can help you…