Home > Article > Backend Development > How to check whether a certain value in an array exists in php
How to query whether it exists: 1. Use the in_array() function, the syntax "in_array(value, $array)"; 2. Use the array_search() function, the syntax "array_search(value, $array)" ".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php query array Whether a value exists
Method 1: Use the in_array() function
The in_array() function can find whether the array contains a certain value , returns TRUE if it exists, and returns FALSE if it does not exist. The syntax format is as follows:
in_array($needle, $array[, $strict = FALSE])
The parameter description is as follows:
Example:
<?php header("Content-type:text/html;charset=utf-8"); $sites = array("Google", "Taobao", "Facebook"); if (in_array("PHP中文网", $sites)) { echo "找到匹配项!"; } else { echo "没有找到匹配项!"; } ?>
##Method 2: Use array_search() function
array_search() function searches for a certain key value in the array. If the specified key value is found in the array, it returns the corresponding key name, otherwise it returns FALSE. The syntax format is as follows:array_search(value,array,strict)
<?php header("Content-type:text/html;charset=utf-8"); $sites = array("Google", "Taobao", "Facebook"); if (array_search("Taobao", $sites)) { echo "找到匹配项!"; } else { echo "没有找到匹配项!"; } ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to check whether a certain value in an array exists in php. For more information, please follow other related articles on the PHP Chinese website!