Home > Backend Development > PHP Tutorial > PHP array function sequence in_array() - Find whether a specified value exists in an array_PHP tutorial

PHP array function sequence in_array() - Find whether a specified value exists in an array_PHP tutorial

WBOY
Release: 2016-07-21 15:23:54
Original
954 people have browsed it

in_array() definition and usage

in_array() function finds whether the specified value exists in the array.

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

Copy code The code is as follows:

$people = array("Peter", "Joe", "Glenn", "Cleveland");

if (in_array("Glenn",$people))
{
echo "Match found";
}
else
{
echo "Match not found";
}
?>

Output:

Match foundExample 2
Copy code The code is as follows:

$people = array("Peter" , "Joe", "Glenn", "Cleveland", 23);

if (in_array("23",$people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}if (in_array("Glenn",$people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}if ( in_array(23,$people, TRUE))
{
echo "Match found
";
}
else
{
echo "Match not found
";
}
?>

Output:
Match not found
Match found
Match found

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324423.htmlTechArticlein_array() definition and usage in_array() function finds whether the specified value exists in the array. Syntax in_array(value,array,type) Parameter Description value Required. Specifies the value to search for in the array. a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template