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 :  Laravel Firebase Push Notification Example

  Example of Laravel Arr::flatten() function


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);

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

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



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…

Read Also :  Laravel Insert Record using jQuery Ajax