Check if value is in array php

[PHP 4, PHP 5, PHP 7, PHP 8]

in_arrayChecks if a value exists in an array

Description

in_array[mixed $needle, array $haystack, bool $strict = false]: bool

Parameters

needle

The searched value.

Note:

If needle is a string, the comparison is done in a case-sensitive manner.

haystack

The array.

strict

If the third parameter strict is set to true then the in_array[] function will also check the types of the needle in the haystack.

Note:

Prior to PHP 8.0.0, a string needle will match an array value of 0 in non-strict mode, and vice versa. That may lead to undesireable results. Similar edge cases exist for other types, as well. If not absolutely certain of the types of values involved, always use the strict flag to avoid unexpected behavior.

Return Values

Returns true if needle is found in the array, false otherwise.

Examples

Example #1 in_array[] example

The second condition fails because in_array[] is case-sensitive, so the program above will display:

Example #2 in_array[] with strict example

Chủ Đề