Query method: 1. Use the in_array() function, the syntax "in_array(value, array)", if included, return TRUE; 2. Use the array_search() function, the syntax "array_search(value, array)" , if included, returns the corresponding key name.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php query whether it is included Specify the array value
Method 1: Use the in_array() function
The in_array() function searches whether the specified value exists in the array. Returns TRUE if the value is found in the array, FALSE otherwise.
<?php header("Content-type:text/html;charset=utf-8"); $people = array("Bill", "Steve", "Mark", "David"); if (in_array("23", $people, TRUE)) { echo "匹配已找到<br>"; } else { echo "匹配未找到<br>"; } if (in_array("Mark",$people, TRUE)) { echo "匹配已找到<br>"; } else { echo "匹配未找到<br>"; } if (in_array(23,$people, TRUE)) { echo "匹配已找到<br>"; } else { echo "匹配未找到<br>"; } ?>
Method 2: Use the array_search() function
array_search() function to search for a key value in the array, and Return the corresponding key name.
If the specified key value is found in the array, return the corresponding key name, otherwise return FALSE. If a key value is found more than once in the array, the key name matching the first found key value is returned.
<?php header("Content-type:text/html;charset=utf-8"); $a = array("2", 1, "3", 4); if (array_search(1,$a,true)){ echo "匹配已找到<br>"; } else{ echo "匹配未找到<br>"; } if (array_search("1",$a,true)){ echo "匹配已找到<br>"; }else{ echo "匹配未找到<br>"; } if (array_search(5,$a,true)){ echo "匹配已找到<br>"; }else{ echo "匹配未找到<br>"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query in php whether it contains a specified array value. For more information, please follow other related articles on the PHP Chinese website!