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.
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 $value
is the value to be found and $array
is the array to be found . The function returns true or false.
Suppose we have an array $fruits
which contains the names of some fruits:
$fruits = array("apple", "banana", "mango", "kiwi", "orange");
Now we want to check if three different values are in this array:
$value1 = "banana"; $value2 = "grape"; $value3 = "orange";
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 不是一个水果."; }
After executing this code, we will get the following results:
banana 是一个水果. grape 不是一个水果. orange 是一个水果.
This code first uses in_array( ) function checks whether $value1
is in the $fruits
array. Since "banana" appears in the array, the condition is true and "banana is a fruit." is output. The next two conditions check whether $value2
and $value3
appear in the $fruits
array. 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 $fruits
array:
$values = array("banana", "grape", "orange", "peach"); foreach ($values as $value) { if (in_array($value, $fruits)) { echo "$value 是一个水果. "; } else { echo "$value 不是一个水果. "; } }
The output result is:
banana 是一个水果. grape 不是一个水果. orange 是一个水果. peach 不是一个水果.
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 数组中 "; // 不打印,因为类型不匹配 }
The output result is:
'1' 存在于 haystack 数组中
The second in_array() expression enables the third parameter true
, which means comparing values and types. Since only one value in the $haystack
array is the number 1, and we are looking for a string "1", the second condition will not hold.
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' 键 "; }
The output result is:
数组中存在 'a' 键 数组中没有 'd' 键
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!