Definition
The in_array() function searches an array for a specific value. 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
Parameter | Description |
---|---|
search |
Required. Specifies the what to search for. |
array |
Required. Specifies the array to search. |
type |
Optional. If this parameter is set to true , the in_array() function searches for the search-string and specific type in the array. |
Example
<?php
// Example 1
$cities = array("New York", "Berlin", "Tokyo");
if(in_array("Tokyo", $cities)) {
echo "Match found!";
} else {
echo "No match found!";
}
echo "<br>";
// Example 2
$numbers = array(5, 7, "10", 12, 15, "18", 20);
if (in_array("15", $numbers, true)) {
echo "Match found!";
} else {
echo "No match found!";
}
echo "<br>";