Laravel Arr where() function Example

In this example, we learn laravel Array where() function example. We will show how to use laravel Arr where() function in laravel. The laravel Arr::where() function to you can filter array in laravel.

Example : How to use laravel Arr where() 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 = [100, 'Laravel', 300, 'Php', 500];
        $filtered = Arr::where($array, function ($value, $key) {
                return is_string($value);
        });

        print_r($filtered);
        //[1 => 'Laravel', 3 => 'Php']

        $array2 = [100, 'Laravel', 300, 'PHP', 500];

        $filtered2 = Arr::where($array2, function ($value, $key) {
            return is_int($value);
        });

        print_r($filtered2);
        // [1 => '200', 3 => '400']
    }
}

Output

Array ( [1] => Laravel [3] => Php ) 
Array ( [0] => 100 [2] => 300 [4] => 500 )

I hope you understand laravel Arr::where() function and it can help you…