Home  >  Article  >  Backend Development  >  How to check whether a certain value belongs to an array in php

How to check whether a certain value belongs to an array in php

PHPz
PHPzOriginal
2023-04-26 10:34:49699browse

In PHP, querying whether a value belongs to an array is a very basic operation. This article will introduce three different methods to determine whether a value belongs to an array.

First method: use the in_array() function

PHP provides an in_array() function, which can be used to determine whether a value belongs to an array. This function has two parameters: the first parameter is the value to be queried, and the second parameter is the array to be queried. The function will return true if the first argument belongs to the second argument, false otherwise.

The following is an example:

<?php
$fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
if (in_array(&#39;apple&#39;, $fruits)) {
    echo &#39;apple belongs to fruits array&#39;;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array

Second method: use array_search() function

PHP also provides another function array_search(), which can be used to query whether a value belongs to an array, and this function can also return the position of the value in the array. If the query is successful, this function will return the key name of the value in the array, otherwise it will return false.

The following is an example:

<?php
$fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
$search = array_search(&#39;apple&#39;, $fruits);
if ($search !== false) {
    echo &#39;apple belongs to fruits array, and its key is &#39; . $search;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array, and its key is 0

Please note that if false is found in the array , 0 or '', will also return false, so we need to use the identity operator (===) to distinguish between returning false and returning 0.

Third method: Use isset() function

isset() function is used to determine whether a variable exists and whether the variable has a value set. Because in an array, whether a certain key has been set to a certain value is often the result of whether we want the query. Therefore, we can also use the isset() function to determine whether a value belongs to an array. The isset() function will return true if the variable has been set to a certain value, otherwise it will return false.

The following is an example:

<?php
$fruits = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;);
if (isset($fruits[array_search(&#39;apple&#39;, $fruits)])) {
    echo &#39;apple belongs to fruits array&#39;;
} else {
    echo &#39;apple does not belong to fruits array&#39;;
}
?>

The above code will output: apple belongs to fruits array

Please note that since the array_search() function is used in this example, We have judged the results to avoid certain unforeseen errors.

The above is the detailed content of How to check whether a certain value belongs to an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn