Home >Backend Development >PHP Problem >How to determine if there is no certain value in an array in php

How to determine if there is no certain value in an array in php

PHPz
PHPzOriginal
2023-04-25 09:09:33560browse

In PHP, it is very easy to determine whether an array contains a value. Usually we use in_array function to check whether a certain value exists in an array. So what should you do if you want to make sure that there is no certain value in the array?

If you look up the in_array function from PHP's official documentation, you will find that the return value of this function is a Boolean value (true or false). If the specified value is found in the array, it returns true, otherwise it returns false. That is, if the in_array function returns false, it means that the value is not in the array. As shown in the following example:

$fruits = array("apple", "banana", "orange");
if (!in_array("pear", $fruits)) {
  echo "数组中没有 pear";
}

In the above example, we create an array of fruits $fruits and use the in_array function to check whether the value "pear" exists in the array. If it does not exist, it will output "There is no pear in the array".

In addition to the in_array function, we can also use the array_search function to determine whether a value exists in the array. The array_search function searches for the specified value in the array and returns the key name of the value if found, otherwise it returns false. So if the array_search function returns false, it means that the specified value is not in the array. As shown in the following example:

$fruits = array("apple", "banana", "orange");
if (array_search("pear", $fruits) === false) {
  echo "数组中没有 pear";
}

In the above example, we also created a fruit array $fruits, but this time we use the array_search function to check whether the value "pear" exists in the array. Unlike the in_array function, if the specified value is found, the array_search function returns the corresponding key name. Therefore, we need to use three equal signs (===) to ensure that the return value is false, because if the return value is 0 (that is, the key name of the first element of the array), the if statement will interpret it as false, This makes it impossible to correctly determine whether the value exists.

In addition to in_array and array_search, there is a simpler way to determine whether an array contains a certain value, which is to use the array_diff function. The array_diff function is used to calculate the difference between an array and an array, that is, it returns values ​​that appear in the first array but not in subsequent arrays. If all values ​​in the first array appear in subsequent arrays, an empty array is returned, that is, if the array_diff function returns an empty result, it means that the array contains the specified value. On the other hand, if the result is not empty, it means that the value does not exist in the array. As shown in the following example:

$fruits = array("apple", "banana", "orange");
if (count(array_diff(array("pear"), $fruits)) > 0) {
  echo "数组中没有 pear";
}

In the above example, we use the array_diff function to compare the $fruits array with an array containing only "pear". If the result is not empty, output "There is no pear in the array".

To sum up, we can use the in_array, array_search or array_diff function to determine whether the array contains the specified value. Among them, the in_array and array_search functions are used to check whether the specified value exists in the array, while the array_diff function is used to calculate the difference between an array and an array. We can easily determine whether an array contains a certain value by simply using these functions with an if statement.

The above is the detailed content of How to determine if there is no certain value in 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