PHP in_array() function checks whether a certain value exists in the array, and returns if it exists TRUE, otherwise returns FALSE.
Syntax:
bool in_array( mixed needle, array array [, bool strict] )
Parameter description:
参数 | 说明 |
---|---|
needle | 需要在数组中搜索的值,如果是字符串,则区分大小写 |
array | 需要检索的数组 |
strict | 可选,如果设置为 TRUE ,则还会对 needle 与 array 中的值类型进行检查 |
Example:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("a", $arr_a)){ echo '字符 a 在 $arr_a 数组中存在'; } else { echo '字符 a 在 $arr_a 数组中不存在'; } ?>
The example output results are as follows:
Example of strict check for character a in $arr_a array:
<?php $arr_a = array("a", "b", "c", 1); if(in_array("1", $arr_a, TRUE)){ echo '字符 1 在 $arr_a 数组中存在'; } else { echo '字符 1 在 $arr_a 数组中不存在'; } ?>
The example output results are as follows:
Character 1 does not exist in the $arr_a array. Example of array as needle:
<?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 数组中不存在'; } ?>
The example output results are as follows:
Array $arr_b exists in array $arr_a
The above article briefly talks about PHP checking whether a certain value exists in the array. The in_array function is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia.