Home >Backend Development >PHP Tutorial >Introduction to how to use the PHP in_array() function

Introduction to how to use the PHP in_array() function

WBOY
WBOYOriginal
2023-06-27 09:40:0110470browse

PHP's in_array() function is a very convenient and practical array function that can help us quickly detect whether a value exists in the array. In this article, we will introduce in detail how to use the in_array() function, as well as some usage tips.

1. The syntax of the in_array() function

The syntax of the in_array() function is as follows:

in_array($needle, $haystack, $strict);

Among them, the $needle parameter represents the value that needs to be detected, and $haystack The parameter represents the array to be detected, and the $strict parameter is an optional parameter indicating whether to enable strict mode. By default, the value of the $strict parameter is false, so it can be omitted.

2. Return value of in_array() function

When the detected value exists in the array, the in_array() function returns true, otherwise it returns false.

3. Usage examples

In this section, we will use several examples to show how to use the in_array() function.

(1) Detect whether a string exists in an array

We can use the in_array() function to detect whether a string exists in an array. The sample code is as follows:

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

$result = in_array("banana", $fruits);

if ($result) {
    echo "该水果存在于数组中";
} else {
    echo "该水果不存在于数组中";
}

The output result is:

该水果存在于数组中

(2) Detect whether a number exists in the array

Similarly, we can use the in_array() function to detect whether a number exists in the array, example The code is as follows:

$numbers = array(3, 6, 9, 12, 15);

$result = in_array(6, $numbers);

if ($result) {
    echo "该数字存在于数组中";
} else {
    echo "该数字不存在于数组中";
}

The output result is:

该数字存在于数组中

(3) Turn on strict mode

By default, the in_array() function does not turn on strict mode. We can enable strict mode by setting the $strict parameter to true. The sample code is as follows:

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

$result1 = in_array(0, $fruits);
$result2 = in_array(0, $fruits, true);

var_dump($result1);
var_dump($result2);

The output result is:

bool(true)
bool(false)

(4) Data type of the detected value

In strict mode, the in_array() function not only checks whether the values ​​are equal, but also checks whether the data types of the values ​​are the same. The sample code is as follows:

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

$result1 = in_array("0", $fruits);
$result2 = in_array("0", $fruits, true);

var_dump($result1);
var_dump($result2);

The output result is:

bool(true)
bool(false)

IV. Techniques

In this section, we will introduce some techniques for using the in_array() function.

(1) Detect whether a value exists in the array and return its key name

Use the array_search() function to quickly detect whether a value exists in the array and return its key name. If the value does not exist in the array, returns false. The sample code is as follows:

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

$result = array_search("banana", $fruits);

if ($result !== false) {
    echo "该水果的键名为:" . $result;
} else {
    echo "该水果不存在于数组中";
}

The output result is:

该水果的键名为:1

(2) Detect whether the value does not exist in the array

In some cases, we need to detect whether a value does not exist in the array. This can be achieved using the negation operator (!) and the in_array() function. The sample code is as follows:

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

if (!in_array("grape", $fruits)) {
    echo "该水果不存在于数组中";
}

The output result is:

该水果不存在于数组中

5. Summary

in_array() function is a very practical array function that can help us quickly detect a value exists in the array. When using this function, you need to pay attention to turning on strict mode and data type detection. In addition, we can also use the array_search() function to quickly detect whether a value exists in the array and return the corresponding key name.

The above is the detailed content of Introduction to how to use the PHP in_array() function. 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