M
M
Mr. Abdu Jan2014-03-26 18:16:47
PHP
Mr. Abdu Jan, 2014-03-26 18:16:47

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

3 answer(s)
A
Alexey Ostin, 2014-03-26
@Abdukhafiz

To solve your problem, there is a function array_search , which searches through the values ​​of an array.

// вернет ключ в массиве, или false, если элемент не найден
array_search($var, $array);

To check for availability should be used like this:
if (array_search($var, $array) !== false) {
  // элемент существует 
} else {
  // элемент не найден
}

If you often have entry-level questions, it's best to go to some conference in jabber/irc/etc. For example, jabber-conference [email protected] I also recommend bookmarking the link to the official PHP documentation.

I
Ivan Boldyrev, 2014-03-26
@iboldurev

Can it be like thisin_array($var, $array);

A
Alexander Khirenko, 2014-03-26
@Satanpit

www.php.su/array_search()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question