In this tutorial you will learn about the Laravel Arr flatten() function Example and its application with practical example.
Laravel Arr flatten() function Example
In this article, I’ll show you how to use laravel Arr flatten() function with example. We will show example of Arr flatten() function in laravel. In this tutorial, we will use Arr flatten() function to flatten a multi-dimensional array into a single level array.
Laravel Arr::flatten method
The Arr::flatten method flattens a multi-dimensional array into a single level array.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\FileController; use Illuminate\Support\Arr; class HomeController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Contracts\Support\Renderable */ public function index() { $array1 = ['name' => 'Joe', 'languages' => ['PHP', 'Ruby']]; $flattened1 = Arr::flatten($array1); print_r($flattened1); echo "<br>"; //['Joe', 'PHP', 'Ruby'] $array2 = ['car' => 'BMW', 'bike' => ['ktm', 'bajaj']]; $flattened2 = Arr::flatten($array2); print_r($flattened2); //['BMW', 'ktm', 'bajaj'] } } |
Output:-
1 2 |
Array ( [0] => Joe [1] => PHP [2] => Ruby ) Array ( [0] => BMW [1] => ktm [2] => bajaj ) |