PHP function primer: in_array()

王林
Release: 2023-06-20 11:12:02
Original
656 people have browsed it

PHP function primer: in_array()

When programming in PHP, using built-in functions can make the code simpler and more efficient. Among them, the in_array() function is a very practical function that can be used to determine whether a specific value appears in an array.

What is the in_array() function?

in_array() is a built-in function of PHP, used to determine whether a specific value exists in an array. Its basic usage is simple:in_array($value, $array), where$valueis the value to be found and$arrayis the array to be found . The function returns true or false.

How to use in_array() function?

Suppose we have an array$fruitswhich contains the names of some fruits:

$fruits = array("apple", "banana", "mango", "kiwi", "orange");
Copy after login

Now we want to check if three different values are in this array:

$value1 = "banana"; $value2 = "grape"; $value3 = "orange";
Copy after login

We can use the in_array() function respectively:

if (in_array($value1, $fruits)) { echo "$value1 是一个水果."; } else { echo "$value1 不是一个水果."; } if (in_array($value2, $fruits)) { echo "$value2 是一个水果."; } else { echo "$value2 不是一个水果."; } if (in_array($value3, $fruits)) { echo "$value3 是一个水果."; } else { echo "$value3 不是一个水果."; }
Copy after login

After executing this code, we will get the following results:

banana 是一个水果. grape 不是一个水果. orange 是一个水果.
Copy after login

This code first uses in_array( ) function checks whether$value1is in the$fruitsarray. Since "banana" appears in the array, the condition is true and "banana is a fruit." is output. The next two conditions check whether$value2and$value3appear in the$fruitsarray. Since "grape" is not in the array, the output "grape is not a fruit."; and "orange" is in the array, so the output is "orange is a fruit.".

We can also use an array to check whether multiple values are in the$fruitsarray:

$values = array("banana", "grape", "orange", "peach"); foreach ($values as $value) { if (in_array($value, $fruits)) { echo "$value 是一个水果. "; } else { echo "$value 不是一个水果. "; } }
Copy after login

The output result is:

banana 是一个水果. grape 不是一个水果. orange 是一个水果. peach 不是一个水果.
Copy after login

in_array( )More usage of the function

The third parameter: strict

in_array() The third parameter of the function is a Boolean value used to control whether type comparison is enabled. By default, it is false (not enabled), so in_array() only compares values and not types.

$haystack = array(1, "1", "2"); if (in_array("1", $haystack)) { echo "'1' 存在于 haystack 数组中 "; // 打印 } if (in_array("1", $haystack, true)) { echo "'1' 存在于 haystack 数组中 "; // 不打印,因为类型不匹配 }
Copy after login

The output result is:

'1' 存在于 haystack 数组中
Copy after login

The second in_array() expression enables the third parametertrue, which means comparing values and types. Since only one value in the$haystackarray is the number 1, and we are looking for a string "1", the second condition will not hold.

Search for key names instead of key values

The in_array() function can only search for key values, but there is also a function array_key_exists() in PHP that can search whether a key name is in the array appeared.

$array = array("a" => "apple", "b" => "banana", "c" => "orange"); if (array_key_exists("a", $array)) { echo "数组中存在 'a' 键 "; } else { echo "数组中没有 'a' 键 "; } if (array_key_exists("d", $array)) { echo "数组中存在 'd' 键 "; } else { echo "数组中没有 'd' 键 "; }
Copy after login

The output result is:

数组中存在 'a' 键 数组中没有 'd' 键
Copy after login

Summary

The in_array() function is a very commonly used function in PHP programming, used to check whether a value appears in an array Pass. Its basic usage is very simple, just pass in the value and array you want to find. For situations where you need to check whether a key name exists, you need to use the array_key_exists() function.

The above is the detailed content of PHP function primer: in_array(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!