Thursday, 25 September 2014

in_array in PHP

PHP in_array() function takes to arguments a component and array Returns true if the component is held as a value in the array, false otherwise.

Syntax:

in_array(search,array,type)
 
Parameters Description: 
search : Required. Determines the what to search for
array : Required. Specifies the array to search
type : Optional. Assuming that this parameter is set, the PHP in_array() function looks for the search-string and particular type in the array.
Note : If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

Example:

<?php
$daysarr = array( "Sunday","Monday","Tuesday","Wednesday","Thursday" );
if ( in_array("Tuesday",$daysarr) )
{
echo "Match found<br />";
}
else
{
echo "Match not found <br />";
}
$daysarr = array( "Sunday","Monday","Tuesday","Wednesday","Thursday", 9 );
if( in_array("9",$daysarr, TRUE) )
{
echo "Match found <br />";
}
else
{
echo "Match not found <br />";
}
if ( in_array("Monday",$daysarr, TRUE))
{
echo "Match found <br />";
}
else
{
echo "Match not found <br />";
}
if ( in_array(9,$daysarr, TRUE) )
{
echo "Match found <br />";
}
else
{
echo "Match not found <br />";
}
?>

O/P

Match found 
Match not found 
Match found
Match found

0 comments:

Post a Comment