Home >Backend Development >PHP Problem >How to determine if a value is in an array in php
In PHP, you can use the in_array function to determine whether a value is in an array. The function of in_array is to search whether a specified value exists in the array. The syntax of this function is "in_array(search,array,type) ”, where the parameter search specifies the value to be searched in the array.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
in_array() function searches whether the specified array exists value.
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
Syntax
in_array(search,array,type)
Parameters
search Required. Specifies the value to search for in the array.
array Required. Specifies the array to search.
type Optional. If this parameter is set to true, it is checked whether the type of the searched data and the value of the array are the same.
Description
Returns true if the given value search exists in the array array. If the third parameter is set to true, the function returns true only if the element exists in the array and has the same data type as the given value.
If the parameter is not found in the array, the function returns false.
Note: If the search parameter is a string and the type parameter is set to true, the search is case-sensitive.
Return value: TRUE if the value is found in the array, FALSE otherwise.
PHP Version: 4
Changelog: As of PHP 4.2, the search parameter may now also be an array.
【Recommended learning: PHP video tutorial】
Example
Use all parameters:
<?php $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>"; } ?>
Running result:
匹配未找到 匹配已找到 匹配未找到
The above is the detailed content of How to determine if a value is in an array in php. For more information, please follow other related articles on the PHP Chinese website!