in_arrayCHECKS if a given value exists in an ARRAY.
This function returns TRUE on SUCCESS, FALSE otherwise.
<?php
bool in_array ( mix $needle , arr $haystack [, bool $strict = FALSE ] )
where,
$needle = The value to be searched
$haystack = The array where the search will be performed
$strict = To control if the check is made over types or not
?>
$needle
The searched value.
If $needle is a STRING, the search is done by the case-sensitive manner.
$haystack
The array where the search will be performed.
$strict
To control if the check is made over types or not.
STRICT MODE |
VALUES |
PURPOSE |
0 (FALSE) |
TYPES WILL NOT BE CHECKED |
1 (TRUE) |
TYPES WILL BE CHECKED |
ed48 |
EXERCISE
<?php
$haystack01 = [ 'THIRTY', 'forty', '88', 99 ];
$needle01 = 'thirty';
// lowercase
$tst01 = in_array($needle01, $haystack01);
if ($tst01 == true)
{
echo 'The needle: "' . $needle01 .
'"<br>WAS FOUND in haystack: <br>';
print_r($haystack01);
}
else
{
echo 'The needle: "' . $needle01 .
'"<br>WAS NOT FOUND in haystack: <br>';
print_r($haystack01);
}
?>
RESULT
The needle: "thirty"
WAS NOT FOUND in haystack:
Array ( [0] => THIRTY [1] => forty [2] => 88 [3] => 99 )
EXERCISE
<?php
$haystack02 = [ 'THIRTY', 'forty', '88', 99 ];
$needle02 = 'THIRTY';
// UPPERCASE
$tst02 = in_array($needle02, $haystack02);
if ($tst02 == true)
{
echo 'The needle: "' . $needle02 .
'"<br>WAS FOUND in haystack: <br>';
print_r($haystack02);
}
else
{
echo 'The needle: "' . $needle02 .
'"<br>WAS NOT FOUND in haystack: <br>';
print_r($haystack02);
}
?>
RESULT
The needle: "THIRTY"
WAS FOUND in haystack:
Array ( [0] => THIRTY [1] => forty [2] => 88 [3] => 99 )
EXERCISE
<?php
$haystack03 = [ 'THIRTY', 'forty', '88', 99 ];
$needle03 = '99';
// '99' STRING COMPARED TO 99 NUMBER
$tst03 = in_array($needle03, $haystack03, true);
if ($tst03 == true)
{
echo 'The needle: "' . $needle03 .
'" treated as STRING...<br>WAS FOUND in haystack: <br>';
print_r($haystack03);
}
else
{
echo 'The needle: "' . $needle03 .
'" treated as STRING...<br>WAS NOT FOUND in haystack: <br>';
print_r($haystack03);
}
?>
RESULT
The needle: "99" treated as STRING...
WAS NOT FOUND in haystack:
Array ( [0] => THIRTY [1] => forty [2] => 88 [3] => 99 )
EXERCISE
<?php
$haystack04 = ['continents' => ['South America', 'Europe'],
'countries' => ['Brasil', 'Portugal']];
$needle04 = ['Brasil', 'Portugal'];
$tst04 = in_array($needle04, $haystack04, true);
if ($tst04 == true)
{
echo 'The needle:<br>';
var_export( $needle04 );
echo '<br>WAS found in the haystack:<br>';
print_r($haystack04);
}
else
{
echo 'The needle:<br>';
var_export( $needle04 );
echo '<br>WAS NOT found in the haystack:<br>';
print_r($haystack04);
}
?>
RESULT
The needle: array ( 0 => 'Brasil', 1 => 'Portugal', )
WAS NOT FOUND in haystack:
Array ( [continents] => Array ( [0] => South America [1] => Europe ) [countries] => Array ( [0] => Brasil [1] => Portugal ) )
EXERCISE
<?php
$haystack05 = ['continents' => ['South America', 'Europe'],
'countries' => ['Brasil', 'Portugal']];
$needle05 = ['Brasil', 'Portugal'];
$tst05 = in_array($needle05, $haystack05, false);
if ($tst05 == true)
{
echo 'The needle:<br>';
var_export( $needle05 );
echo '<br>WAS found in the haystack:<br>';
print_r($haystack05);
}
else
{
echo 'The needle:<br>';
var_export( $needle05 );
echo '<br>WAS NOT found in the haystack:<br>';
print_r($haystack05);
}
?>
RESULT
The needle: array ( 0 => 'Brasil', 1 => 'Portugal', )
WAS NOT FOUND in haystack:
Array ( [continents] => Array ( [0] => South America [1] => Europe ) [countries] => Array ( [0] => Brasil [1] => Portugal ) )
EXERCISE
<?php
$haystack06 = 1234567;
$needle06 = 123;
$tst06 = in_array($needle06, $haystack06, true);
if ($tst06 == true)
{
echo 'The needle:<br>';
var_export( $needle06 );
echo '<br>WAS found in the haystack:<br>';
print_r($haystack06);
}
else
{
echo 'The needle:<br>';
var_export( $needle06 );
echo '<br>WAS NOT found in the haystack:<br>';
print_r($haystack06);
}
?>
RESULT
In PHP 7.4.XX it displays a Warning: ...,
in PHP 8.0.XX a Fatal error: ... will be issued.
Run this code to see these occurrences.