The following programs show how to determine if an array contains a particular value:
Program 1:
// array contains elements
$names
=
array
(
’Geeks ’
,
’ for’
,
’Geeks’
);
// element search
$item
=
’Geeks’
;
if
(in_array (
$item
,
$names
)) {
// there is an item
echo
"Your element founded"
;
}
else
{
// no item
echo
"Element is not found"
;
}
?>
Output:Your element founded
Program 2:In this example, the in_arrayfunction works in strict mode, it also checks the type of array elements. $name
=
array
(
" Python "
,
"Java"
,
"C # "
, 3);
// Find python
if
(in_array (
" python "
,
$name
, TRUE))
{
echo
" found "
;
}
else
{
echo
"not found"
;
}
// Seraching 3
if
(in_array (3,
$name
, TRUE))
{
echo
"found"
;
}
else
{
echo
"not found"
;
}
// Seraching Java
if
(in_array (
"Java"
,
$name
, TRUE))
{
echo
"found"
;
}
else
{
echo
"not found"
;
}
?>
Output:not found found found
Program 3: $arr
=
array
(
’gfg’
, 1, 17);
if
(in_array (
’ gfg’
,
$arr
)) {
echo
"gfg"
;
}
if
(in_array (18,
$arr
)) {
echo
"18 found with strict check"
;
}
?>
Output:gfg
In the above example, the array contains the string "gfg" and two numbers 1 and 17. The if statement calls the in_array() functionwhich checks if the given element as a parameter in the given array or not. If the function returns true, the if statement is executed.