In this tutorial you will learn about the Laravel Arr where() function Example and its application with practical example.
Laravel Arr where() function Example
In this article, I’ll show you how to use laravel Arr where() function with example. We will show example of Arr where() function in laravel. In this tutorial, we will use Arr where() function to filter an array using the provided where Closure.
Laravel Arr::where method
The Arr::where method filters an array using the provided where Closure.
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 32 33 34 35 |
<?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() { $array = [100, 'Laravel', 300, 'Php', 500]; $filtered = Arr::where($array, function ($value, $key) { return is_string($value); }); print_r($filtered); //[1 => 'Laravel', 3 => 'Php'] echo "<br>"; $array2 = [100, '200', 300, '400', 500]; $filtered2 = Arr::where($array2, function ($value, $key) { return is_string($value); }); print_r($filtered2); // [1 => '200', 3 => '400'] } } |
Output:-
1 2 |
Array ( [1] => Laravel [3] => Php ) Array ( [1] => 200 [3] => 400 ) |