Answer the question
In order to leave comments, you need to log in
How to check the value of an array with a variable?
Hello.
There is an array $arr[2] and a variable $var = "Two";
It has the following data:
- $arr[0] = "One";
- $arr[1] = "Two";
- $arr[2] = "Three";
You need to check if at least one value of the array is equal to the variable.
PS Programming language php.
Answer the question
In order to leave comments, you need to log in
To solve your problem, there is a function array_search , which searches through the values of an array.
// вернет ключ в массиве, или false, если элемент не найден
array_search($var, $array);
if (array_search($var, $array) !== false) {
// элемент существует
} else {
// элемент не найден
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question