Home  >  Article  >  Backend Development  >  How to check if a php array contains a certain value

How to check if a php array contains a certain value

PHPz
PHPzOriginal
2023-04-27 09:02:43494browse

PHP is a popular server-side programming language that supports various data types. Arrays, as a commonly used data type, are widely used in development. For array operations, determining whether a certain value is contained is a necessary skill. This article will introduce how to determine whether an array contains a certain value through PHP.

First, let’s take a look at how to use the in_array() function to determine whether an array contains a certain value. The in_array() function is used to determine whether a value exists in the array, and the return value is of Boolean type.

The sample code is as follows:

$array = array('apple', 'banana', 'cherry');
if (in_array('banana', $array)) {
    echo "存在";
} else {
    echo "不存在";
}

Explain the logic of this code: First we define an array $array, which contains three elements 'apple', 'banana', and 'cherry' . Then we use the in_array() function to determine whether ‘banana’ exists in the array. If it exists, output “exists”, otherwise output “does not exist”. In this example, the output is "exists".

When using the in_array() function, you need to pay attention to the following points:

  1. The first parameter of the in_array() function is the value you need to find, and the second parameter is the value you need to find. The array to search for.
  2. The in_array() function compares all key values ​​of the array by default, so you need to be careful not to confuse key values ​​and key names when using them. The following sample code:

    $array = array('name'=>'tom', 'age'=>18);
    if (in_array(18, $array)) {
        echo "存在";
    } else {
        echo "不存在";
    }

    In this example, the value of the key name 'age' is 18, but the in_array() function does not match the key name, but only the key value. So, in this example, the output is "doesn't exist".

  3. The in_array() function is case-sensitive by default, and the comparison behavior can be changed through the third parameter. If the third parameter is set to true, it means that it is not case sensitive.

    $array = array('apple', 'Banana', 'cherry');
    if (in_array('banana', $array)) {
        echo "存在";
    } else {
        echo "不存在";
    }

    In this example, the output is "does not exist" because the case of 'banana' is inconsistent.

In addition to the in_array() function, you can also use the array_search() function to find whether an array contains a certain value. The array_search() function returns the key name of the value in the array when the specified value is found. If it cannot be found, it returns false.

The sample code is as follows:

$array = array('apple', 'banana', 'cherry');
if (array_search('banana', $array) !== false) {
    echo "存在";
} else {
    echo "不存在";
}

Explain the logic of this code: First we define an array $array, which contains three elements 'apple', 'banana', ' cherry'. Then we use the array_search() function to find whether 'banana' exists in the array. If it exists, it outputs "exists", otherwise it outputs "does not exist". In this example, the output is "exists".

When using the array_search() function, you need to pay attention to the following points:

  1. The first parameter of the array_search() function is the value to be found, and the second parameter is the value to be found. The array to search for.
  2. The array_search() function compares all key values ​​of the array by default, so you need to be careful not to confuse key values ​​and key names when using them.
  3. The array_search() function is case-sensitive by default, and the comparison behavior can be changed through the third parameter. If the third parameter is set to true, it means that it is not case sensitive.

To sum up, judging whether a PHP array contains a certain value can be achieved through the in_array() or array_search() function. It should be noted that when using these functions, you need to pay attention to issues such as data types, differences between key values ​​and key names, and capitalization. Proficient in these skills can improve development efficiency and avoid errors.

The above is the detailed content of How to check if a php array contains a certain value. 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