Laravel Arr hasAny() function Example

In this example, I explain laravel Arr hasAny() function. Laravel Arr:hasAny() function checks whether any item in a given set exists in an array using dot notation

Laravel Arr::hasAny() function is most important when you check any item or key exists in an array using dot notation

Example : Laravel Arr::hasAny() 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 = ['product' => ['name' => 'Desk', 'price' => 100]];

        $contains1 = Arr::hasAny($array, 'product.name');
        if($contains1){
            echo 'Item is exists ';
        }else{
            echo 'Item is not exists ';
        }
        // true
        
        $contains2 = Arr::hasAny($array, ['product.name', 'product.discount']);
        if($contains2){
            echo 'Item is exists ';
        }else{
            echo 'Item is not exists ';
        }
        // true
        
        $contains3 = Arr::hasAny($array, ['category', 'product.discount']);
        if($contains3){
            echo 'Item is exists ';
        }else{
            echo 'Item is not exists ';
        }
        // false
    }
}

routes/web.php

<?php

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

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

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