Home >Backend Development >PHP Tutorial >php check if a value exists in array
The content of this article is about PHP checking whether there is a value in the array. It has certain reference value. Now I share it with you. Friends in need can refer to it
PHP
in_array()
The function checks whether a certain value exists in the array. If it exists, it returns TRUE
, otherwise it returns FALSE
.
Syntax:
bool in_array( mixed needle, array array [, bool strict] )
Parameter description:
Parameter | Description |
---|---|
needle | The value that needs to be searched in the array, if it is a string, it is case-sensitive |
array | Array to be retrieved |
strict | Optional, if set to TRUE, the value types in needle and array will also be checked |
Example:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("a", $arr_a)){ echo '字符 a 在 $arr_a 数组中存在'; } else { echo '字符 a 在 $arr_a 数组中不存在'; } ?>
Character a exists in $arr_a array Strictly checked example:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("1", $arr_a, TRUE)){ echo '字符 1 在 $arr_a 数组中存在'; } else { echo '字符 1 在 $arr_a 数组中不存在'; } ?>
Character 1 in $ Example of array as needle does not exist in arr_a array:
<?php $arr_a = array(array("a", "b"), 1, 2); $arr_b = array("a", "b"); if(in_array($arr_b, $arr_a)){ echo '数组 $arr_b 在 $arr_a 数组中存在'; } else { echo '数组 $arr_b 在 $arr_a 数组中不存在'; } ?>
array$arr_b
exists in $arr_a
array
PHP
in_array()
The function checks whether a certain value exists in the array. If it exists, it returns TRUE
, otherwise it returns FALSE
.
Syntax:
bool in_array( mixed needle, array array [, bool strict] )
Parameter description:
Parameter | Description |
---|---|
needle | The value that needs to be searched in the array, if it is a string, it is case-sensitive |
array | Array to be retrieved |
strict | Optional, if set to TRUE, the value types in needle and array will also be checked |
Example:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("a", $arr_a)){ echo '字符 a 在 $arr_a 数组中存在'; } else { echo '字符 a 在 $arr_a 数组中不存在'; } ?>
Character a exists in the $arr_a array Strictly checked example:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("1", $arr_a, TRUE)){ echo '字符 1 在 $arr_a 数组中存在'; } else { echo '字符 1 在 $arr_a 数组中不存在'; } ?>
Character 1 in $ Example of array as needle does not exist in arr_a array:
<?php $arr_a = array(array("a", "b"), 1, 2); $arr_b = array("a", "b"); if(in_array($arr_b, $arr_a)){ echo '数组 $arr_b 在 $arr_a 数组中存在'; } else { echo '数组 $arr_b 在 $arr_a 数组中不存在'; } ?>
array$arr_b
exists in $arr_a
array
Related recommendations:
PHP method to check whether the IP address is legal
PHP example code to check whether the website is down
The above is the detailed content of php check if a value exists in array. For more information, please follow other related articles on the PHP Chinese website!