Home  >  Article  >  Backend Development  >  How to determine whether a value exists in an array in php

How to determine whether a value exists in an array in php

PHPz
PHPzOriginal
2023-04-20 10:11:39665browse

In PHP programming, it is a very common operation to determine whether a value exists in an array. This article will introduce three methods to achieve this functionality.

Method 1: Use the in_array function

The in_array function is provided in PHP, which can determine whether a value exists in an array. If it exists, it returns TRUE, otherwise it returns FALSE. The syntax of this function is as follows:

bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Among them, $needle represents the value that needs to be checked, $haystack represents the target array, and $strict represents whether strict mode is enabled. The default is FALSE, which means strict mode is not enabled.

The following is an example of using the in_array function to determine whether a value exists in an array:

<?php
$colors = array("red", "green", "blue");
if (in_array("green", $colors)) {
    echo "存在";
} else {
    echo "不存在";
}
?>

The output result is: exists

Method 2: Use the array_search function

The array_search function in PHP is used to find a value in an array and return the key name. If the value is not in the array, returns FALSE. The syntax of this function is as follows:

mixed array_search ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

Among them, $needle represents the value that needs to be checked, $haystack represents the target array, and $strict represents whether strict mode is enabled. The default is FALSE, which means strict mode is not enabled.

The following is an example of using the array_search function to determine whether a value exists in an array:

<?php
$colors = array("red", "green", "blue");
$key = array_search("green", $colors);
if ($key !== false) {
    echo "存在";
} else {
    echo "不存在";
}
?>

The output result is: exists

Note: When using the array_search function, It is necessary to determine whether the return value is strictly equal to FALSE, because it is possible to return 0, and the key name corresponding to 0 is also false.

Method 3: Use isset function

The isset function in PHP is used to detect whether a variable has been set and is not NULL. Returns TRUE if the variable exists and is non-NULL, otherwise returns FALSE. Because key values ​​that do not exist in the array are considered NULL, you can use the isset function to determine whether a value exists in the array. The following is an example of using the isset function to determine whether a value exists in an array:

<?php
$colors = array("red", "green", "blue");
if (isset($colors[array_search("green", $colors)])) {
    echo "存在";
} else {
    echo "不存在";
}
?>

The output result is: exists

In this example, the array_search function is used to obtain "green" in the array The corresponding key name in the key, and then use the isset function to determine whether the key value already exists.

Among these three methods, it is recommended to use the in_array function. Because its syntax is simple, easy to understand, and does not require judgment on key values. If you need to get the key name corresponding to the value in the array, you can use the array_search function. Although the isset function can also be used to determine whether a certain value exists in an array, it is usually used to detect the existence of a variable and is not suitable for detecting array values.

The above is the detailed content of How to determine whether a value exists 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