Home  >  Article  >  Backend Development  >  How to check if a value is in an array in php

How to check if a value is in an array in php

PHPz
PHPzOriginal
2023-04-27 09:00:57404browse

PHP is an open source scripting language widely used for web development. In PHP, you often operate on arrays, and you often need to query whether a value is in the array.

In PHP, you can use the in_array() function to query whether a value is in an array. The in_array() function accepts two parameters. The first parameter is the value to be queried, and the second parameter is the array to be queried.

The following is the syntax of the in_array() function:

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

Among them, $needle is the value to be queried, $haystack is the array to be queried, and the default value of $strict is FALSE, which means use Non-strict mode.

In non-strict mode, if the queried value is found in the array, the in_array() function will return TRUE; otherwise, it will return FALSE. If the query value is a string, the in_array() function will automatically convert the type, even if the string represents a number.

For example, the following code demonstrates how to use the in_array() function to query whether a value is in an array:

$fruits = array("apple", "banana", "cherry");
if (in_array("apple", $fruits)) {
    echo "apple is in the array";
} else {
    echo "apple is not in the array";
}

In this example, $fruits is an array containing three elements. We use the in_array() function to query whether the string "apple" is in the array $fruits. Since the queried value "apple" is in the array $fruits, the in_array() function returns TRUE, and the program outputs "apple is in the array".

Of course, if the queried value is not in the array, the in_array() function will return FALSE. For example, the following code demonstrates how to query for a value that is not in an array:

$fruits = array("apple", "banana", "cherry");
if (in_array("orange", $fruits)) {
    echo "orange is in the array";
} else {
    echo "orange is not in the array";
}

In this example, $fruits is an array containing three elements. We use the in_array() function to query whether the string "orange" is in the array $fruits. Since the queried value "orange" is not in the array $fruits, the in_array() function returns FALSE, and then the program outputs "orange is not in the array".

It should be noted that the in_array() function will automatically convert the type in non-strict mode. For example, if the value of the query is the string "123" and an element in the array is the number 123, the in_array() function will also return TRUE. If you want to use strict mode, you need to set the $strict parameter to TRUE. In strict mode, the in_array() function compares types and values.

For example, the following code demonstrates how to query whether a value is in an array in strict mode:

$numbers = array(1, 2, 3);
if (in_array("1", $numbers, true)) {
    echo "1 is in the array";
} else {
    echo "1 is not in the array";
}

In this example, $numbers is an array containing three elements. We use the in_array() function to query whether the string "1" is in the array $numbers. Since strict mode is used and "1" is not equal to 1 (different types), the in_array() function returns FALSE, and the program outputs "1 is not in the array".

To sum up, the in_array() function is a common method in PHP for querying whether a value is in an array, and can use non-strict mode or strict mode as needed. Mastering the usage of this function can facilitate array operations and improve PHP programming efficiency.

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