How to determine whether a php array contains a specified value

PHPz
Release: 2023-04-27 15:48:07
Original
680 people have browsed it

In PHP programming, arrays are very commonly used data structures. When operating an array, we sometimes need to determine whether the values ​​in the array contain a specific value. So, how to implement the function of whether the PHP array value contains? Let’s introduce it in detail below.

1. Use the in_array() function

in_array() is one of PHP’s built-in functions, used to determine whether a value is in an array. The syntax is as follows:

    in_array($needle, $haystack, $strict)
Copy after login

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict records whether strict mode is turned on, that is, in addition to judging the value, the data type is also judged.

For example:

    $array = array('apple', 'banana', 'orange');
    if (in_array('apple', $array)) {
        echo '找到了'; // 输出:找到了
    } else {
        echo '没找到';
    }
Copy after login

2. Use the array_search() function

array_search() is also one of PHP’s built-in functions, used to find the key name of a value in an array . If the value does not exist in the array, returns false. The syntax is as follows:

    array_search($needle, $haystack, $strict)
Copy after login

For example:

    $array = array('apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange');
    $result = array_search('red', $array);
    if ($result !== false) {
        echo '找到了,键名是:' . $result; // 输出:找到了,键名是:apple
    } else {
        echo '没找到';
    }
Copy after login

3. Use in_array() function and array_keys() function

You can use in_array() function and array_keys() function at the same time To find the key name of a certain value in the array. Among them, the in_array() function is used to determine whether the value is in the array, and the array_keys() function is used to return the key name corresponding to the value in the array. The syntax is as follows:

    $keys = array_keys($array, $value)
Copy after login

For example:

    $array = array('apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange');
    if (in_array('red', $array)) {
        $keys = array_keys($array, 'red');
        echo '找到了,键名是:' . $keys[0]; // 输出:找到了,键名是:apple
    } else {
        echo '没找到';
    }
Copy after login

It is worth noting that if there are multiple identical values ​​in the array, the array_keys() function returns an array of key names corresponding to all values. And not just the first one.

4. Use the isset() function

Finally, some PHP beginners may use the isset() function to determine whether a value is in an array. However, the main purpose of the isset() function is to determine whether a variable exists, and is not recommended for determining whether a value is in an array.

Because the isset() function only determines whether a certain value is defined, but cannot determine whether it is in the array. For example:

    $array = array('apple' => 'red', 'banana' => 'yellow', 'orange' => 'orange');
    if (isset($array['red'])) {
        echo '找到了'; // 不会输出
    } else {
        echo '没找到';
    }
Copy after login

In the above code, the isset() function will return false, but in fact there is no element with the key name 'red' in the array. Therefore, do not use the isset() function when determining whether a value is in an array.

Summary:

Whether the PHP array value contains or not can be implemented using the in_array() function and array_search() function. It can also be implemented using the array_keys() function and in_array() function, but not It is recommended to use the isset() function to determine whether the value is in the array. In actual development, it is very important to choose the appropriate method to implement whether PHP array values ​​are included according to actual needs.

The above is the detailed content of How to determine whether a php array contains a specified value. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template