PHP Whether the searched value is present in the array - in_array
Colleagues hello to all.
In today's article, we'll look at a special function in PHP that helps determine if a value exists in an array. The function returns TRUE if the given value is found in the array, and FALSE if not found.
Function syntax.
in_array(mixed $value, array $array, bool $strict = false)
- $value - The initial value we want to find in the array.
- $array - The array itself in which we will look for values.
- $strict - Case sensitive, case insensitive by default.
To be honest, I've been using this feature for years, but case sensitivity never worked for me, so it's not certain that the $strict setting works at all.
In the example, we will search for the values kiev in the array of countries.
$country = ['Kiev', 'Poltava', 'America', 'moldova'];
if (in_array('kiev', $country)) {
echo "TRUE";
} else {
echo "FALSE";
}
As a result, we got FALSE, the value was not found. We get FALSE because the lookup value kiev has a lowercase first character.
In the next example, we will use the same example, but the searched value will be the same as in the array.
$country = ['Kiev', 'Poltava', 'America', 'moldova'];
if (in_array('Kiev', $country)) {
echo "TRUE";
} else {
echo "FALSE";
}
As a result, we received TRUE, the values were successfully found.
Thank you all, I hope that my article helped you in some way.