Home > Hosting > Program

How does php judge whether a number is in an array?

2024-04-12 16:34:47

In PHP, there are many methods to determine whether a number is in an array. This article will introduce these methods in detail, and also introduce some other array operation functions.

Use the in_array () function The in_array () function to determine whether a value is in an array. The syntax is as follows:

in_array($value, $array, $strict)

Among them, the $value parameter indicates the value to be searched, the $array parameter indicates the array to be searched, and the $strict parameter indicates whether the data types match strictly, which can be omitted, and the default value is false. Returns true if a value is found, and false otherwise.

The sample code is as follows:

$fruits = array("apple", "banana", "orange"); If (in _ array ("apple", $ fruits)) {echo "Apple exists in the array!" ; } else {echo "Apple doesn't exist in the array!" ; }

The output result is:

Apple exists in the array!

Use the array_search () function array_search () function to find the value specified in the array and return the corresponding key name. If it cannot be found, it will return false. Its syntax is as follows:

array_search($value, $array, $strict)

Among them, the $value parameter indicates the value to be searched, the $array parameter indicates the array to be searched, and the $strict parameter indicates whether the data types match strictly, which can be omitted, and the default value is false.

The sample code is as follows:

$fruits = array("apple", "banana", "orange"); $key = array_search("banana", $fruits); if ($key ! == false) {echo "The key name of banana in the array is:". $key; } else {echo "Banana does not exist in the array!" ; }

The output result is:

The key names of bananas in the array are: 1.

If you want to find out whether a value exists, you can use it as follows:

$fruits = array("apple", "banana", "orange"); if (array_search("orange", $fruits) ! == false) {echo "Oranges exist in the array!" ; } else {echo "Oranges do not exist in the array!" ; }

The output result is:

Oranges exist in the array!

Use in_array () and array_search () to judge at the same time. In some cases, it is necessary to know whether a value is in the array and its key name. At this time, you can combine in_array () and array_search () to judge at the same time.

The sample code is as follows:

$fruits = array("apple", "banana", "orange"); if (in_array("banana", $fruits) && ($key = array_search("banana", $fruits)) ! == false) {echo "The key name of banana in the array is:". $key; } else {echo "Banana does not exist in the array!" ; }

The output result is:

The key names of bananas in the array are: 1.

Use the array_key_exists () function array_key_exists () function to determine whether the specified key name exists in the array. Its syntax is as follows:

array_key_exists($key, $array)

Among them, the $key parameter represents the key name to be searched, and the $array parameter represents the array to be searched. Returns true if the key name is found, otherwise returns false.

The sample code is as follows:

$fruits = array("apple" => 0, "banana" => 1, "orange" => 2); If (array _ key _ exists ("banana", $ fruits)) {echo "Bananas exist in the array!" ; } else {echo "Banana does not exist in the array!" ; }

The output result is:

Bananas exist in the array!

Use isset () function isset () function to judge whether the specified key name exists in the array, and its syntax is as follows:

isset($array[$key])

Among them, the $key parameter represents the key name to be searched, and the $array parameter represents the array to be searched. Returns true if the key name is found, otherwise returns false.

The sample code is as follows:

$fruits = array("apple" => 0, "banana" => 1, "orange" => 2); If (isset ($ fruits ["orange"]) {echo "Oranges exist in the array!" ; } else {echo "Oranges do not exist in the array!" ; }

The output result is:

Oranges exist in the array!

Note: When using the isset () function, it should be noted that if the corresponding value of the key name is null, it will also return false, so it is impossible to accurately judge whether a key name exists in the array.

Use the array_diff () function array_diff () function to find the difference set of two arrays, that is, to return all the values in the first array but not in the following array. Therefore, if the value to be searched is used as the first array and the original array is used as the second array, and the search result is that the difference set is empty, it means that it exists in the original array.

The sample code is as follows:

$fruits = array("apple", "banana", "orange"); If (count (array _ diff (array ("banana"), $ fruits)) = = 0) {echo "Bananas exist in the array!" ; } else {echo "Banana does not exist in the array!" ; }

The output result is:

Bananas exist in the array!

Use the preg_grep () function preg_grep () function to match array elements with regular expressions and return all the matched elements. Therefore, if the value to be searched is used as a regular expression, the original array is used as a parameter, and the search result is a non-empty array, it means that it exists in the original array.

The sample code is as follows:

$fruits = array("apple", "banana", "orange"); If (count (preg _ grep ("/banana/",$ fruits)) > 0) {echo "Bananas exist in the array!" ; } else {echo "Banana does not exist in the array!" ; }

The output result is:

Bananas exist in the array!


Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us