Home >Backend Development >PHP Tutorial >PHP array function sequence in_array() finds whether the array value exists
in_array() Definition and Usage The
in_array() function searches an array for a given value.
Syntax
in_array(value,array,type)
Parameter Description
value 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 value 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 value parameter is a string and the type parameter is set to true, the search is case-sensitive.
Example 1
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn",$people)) { echo "Match found"; } else { echo "Match not found"; } ?>
Output:
Match found
Example 2
<?php $people = array("Peter", "Joe", "Glenn", "Cleveland", 23); if (in_array("23",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; }if (in_array("Glenn",$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; }if (in_array(23,$people, TRUE)) { echo "Match found<br />"; } else { echo "Match not found<br />"; } ?>
Output:
Match not found
Match found
Match found
More PHP array function sequence in_array() search array value If there are related articles, please pay attention to the PHP Chinese website!