Search value exist in array or not using php
In php there is a inbuild function called in_array() which is used to search data is existing or not in array. It returns boolean value i.e "TRUE" if the search value is found in the array, and "FALSE" if the search value is not found.
Syntax :
bool in_array ( $value, $array_name ,$mode )
- $value: This is the compulsory parameter for in_array () function which contains the searching value. This can be of mixed type i.e, it can be of string type or integer type or any other type. it depends on user that which type of search value wants to find in array.
- $array_name: This is a compulsory parameter and it specifies the array in which we want to search.
- $mode: This is third one which is an optional parameter and is of boolean type. This parameter specifies the mode in which we want to perform the search. If it is set to TRUE, then the in_array() function searches for the value with the same type of value as specified by $val parameter. The default value of this parameter is FALSE.
Below programs to illustrate The in_array() function in PHP:
<?php
$marks
=
array
(100, 65, 70, 87);
if
(in_array(
"100"
,
$marks
))
{
echo
"found"
;
}
else
{
echo
"not found"
;
}
?>
in the program we are searching 100 in array marks weather it exist in array or not. after successfull running when its found the 100 in array then function returns "TRUE" as a "found" string message.
Output:
found
No comments:
Post a Comment
If you have any doubts regarding the post. Please let me know.