Has Laravel 11 some function like in_array?
Laravel 11, as of now, does not have a built-in function exactly like PHP's in_array()
. However, Laravel provides several ways to achieve similar functionality using its collection and array methods.
One way to check if an array contains a specific value is by using Laravel's collection contains()
method. Here's an example:
use Illuminate\Support\Facades\Array;
use Illuminate\Support\Collection;
// Assuming $array is an array and $value is the value you want to search for
$result = Array::has($array, $value); // This is an alternative way using Array helper function
// Or using Collection
$collection = collect($array);
$result = $collection->contains($value);
// $result will be true if the value is found in the array, false otherwise
Another way to check if an array contains a specific value is by using Laravel's array in_array()
function equivalent, which is called contains()
as well, but it's located in the array
helper function:
use Illuminate\Support\Facades\Array;
// Assuming $array is an array and $value is the value you want to search for
$result = Array::contains($array, $value);
// $result will be true if the value is found in the array, false otherwise
Both methods provide similar functionality as PHP's in_array()
.