in_array
Check if there is a value in the array
in_array()
function searches for the specified value in the array.
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case sensitive.
Search the value "Glenn" in the array and output some text:
<?php $people = array ( "Bill" , "Steve" , "Mark" , "David" ) ; if ( in_array ( "Mark" , $people ) ) { echo "Match Found" ; } else { echo "Match not found" ; } ?>
Try it yourself
Use all parameters:
<?php $people = array ( "Bill" , "Steve" , "Mark" , "David" ) ; if ( in_array ( "23" , $people , TRUE ) ) { echo "Match Found<br>" ; } else { echo "Match not found<br>" ; } if ( in_array ( "Mark" , $people , TRUE ) ) { echo "Match Found<br>" ; } else { echo "Match not found<br>" ; } if ( in_array ( 23 , $people , TRUE ) ) { echo "Match Found<br>" ; } else { echo "Match not found<br>" ; } ?>
Try it yourself
in_array ( search , array , type )
parameter | describe |
---|---|
Search | Required. Specifies the value to be searched in the array. |
array | Required. Specifies the array to search. |
type | Optional. If this parameter is set to true, check whether the searched data is the same as the value of the array. |
Return true if the given value search 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 the data type is the same as the given value.
If no argument is found in the array, the function returns false.
Note: If the search parameter is a string and the type parameter is set to true, the search is case sensitive.