Home  >  Article  >  Backend Development  >  How to determine if an element is in an array in PHP

How to determine if an element is in an array in PHP

PHPz
PHPzOriginal
2023-04-27 09:01:53523browse

Determining whether an element is in an array is a technique often used in PHP. This application is very wide and can be used in both website development and data analysis.

In PHP, we can use the in_array() function to easily determine whether an element is in an array.

The syntax for using the in_array() function is very simple:

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

$needle represents the element to be found, $haystack represents the array to be searched, and $strict represents whether to use strict mode. The default is false.

Next, we will introduce the in_array() function through practical cases and show you how to check elements in a PHP array.

  1. Determine whether an element exists in an array

The following is a code example that uses the in_array() function to determine whether a string element exists in an array:

$fruits = array("apple", "banana", "cherry", "orange");

if (in_array("apple", $fruits)) {
    echo "apple 存在于fruits数组中";
} else {
    echo "apple 不存在于fruits数组中";
}

Output: apple exists in the fruits array

Running the above code will output "apple exists in the fruits array".

As you can see, in this code example we first declare a "fruits" array containing several fruit names. Then, we use the if-else statement to determine whether "apple" exists in the array based on the judgment condition (if "apple" exists in the array, output a prompt message, otherwise output an error message).

Since "apple" is indeed stored in the array, the program outputs "apple exists in the fruits array".

  1. Determine whether the element does not exist in the array

Use the in_array() function to determine whether the element does not exist in the array. You only need to add a fetch to the above code example. Just use the inverse operator "!". As shown below:

$fruits = array("apple", "banana", "cherry", "orange");

if (!in_array("pear", $fruits)) {
    echo "pear 不存在于fruits数组中";
} else {
    echo "pear 存在于fruits数组中";
}

Output: pear does not exist in fruits array

In this code example, we first declare a "fruits" array containing several fruit names. Then, we use the if-else statement to determine whether "pear" does not exist in the array based on the judgment condition (if "pear" is not in the array, output a prompt message, otherwise output an error message).

Since "pear" is not stored in the array, the program outputs "pear does not exist in the fruits array".

  1. Use strict mode to determine whether an element exists in an array

In PHP, strict comparison can ensure that the data type and value are equal. Therefore, when determining whether an element exists in an array, you can use strict mode to ensure that the data types of the elements are also equal.

In the in_array() function, you can use the third parameter $strict and set its value to true to enable strict mode.

The following is a code example that uses strict mode to determine whether a string element exists in an array:

$fruits = array("apple", "banana", "cherry", "orange");

if (in_array("1", $fruits, true)) {
    echo "1 存在于fruits数组中";
} else {
    echo "1 不存在于fruits数组中";
}

Output: 1 does not exist in fruits array

In this code In the example, we first declare a "fruits" array containing several fruit names. Then, we use the if-else statement to determine whether "1" exists in the array based on the judgment condition (if "1" exists in the array, output a prompt message, otherwise output an error message).

Because we use strict mode, the program outputs "1 does not exist in the fruits array".

  1. Check whether multiple elements exist in the array

If you want to check whether multiple elements exist in the array, we can use a foreach loop to iterate through each element, Use the in_array() function to check if it exists.

The following is a code example that uses a foreach loop to check whether multiple elements exist in an array:

$fruits = array("apple", "banana", "cherry", "orange");

$check_fruits = array("orange", "pear");

foreach ($check_fruits as $fruit) {
    if (in_array($fruit, $fruits)) {
        echo $fruit . " 存在于fruits数组中<br>";
    } else {
        echo $fruit . " 不存在于fruits数组中<br>";
    }
}

Output:
orange exists in fruits array
pear does not exist in fruits In the array

In this code example, we first declare a "fruits" array containing several fruit names, and a "check_fruits" array to be checked.

Then, we use a foreach loop to iterate through each element in the "check_fruits" array. Within the loop, we use the in_array() function to check if the element currently in the loop is already present in the "fruits" array.

Finally, this code example will output a check indicating which elements are present in the "fruits" array and which are not. In this example, it will output "orange exists in the fruits array" and "pear does not exist in the fruits array".

Summary

In PHP, it is a very common requirement to determine whether an element exists in an array. For this purpose, we can use the in_array() function to make a quick and easy judgment. Since it accepts a third parameter $strict, we can also enable strict mode to ensure that the data types of the elements are also equal. Furthermore, if we want to check if multiple elements are present in the array, we can use foreach loop and in_array() function to accomplish this task.

The above is the detailed content of How to determine if an element is 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