Difference between in_array() and array_search() in php?
In this tutorial,we will study difference between in_array and array_search.
in_array : -Checks if a value exists in an array
array_search() :-searches an array for a given value and returns the corresponding key if successful.
in_array Syntax:
in_array(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.
Example:
<?php
$name = array("Rajesh", "Shewale", "PHP", "Linux");
if (in_array("Rajesh", $name))
{
echo "Yes";
}
else
{
echo "False";
}
?>
Output: Yes
array_search() :-searches an array for a given value and returns the corresponding key if successful.
Syntax:
array_search()(search value, array , strict Boolean);
search value:- will be the value to search in an array
array: will the array to search
strict Boolean: is optional.
Example:
<?php
$name = array(0=>"Rajesh", 1=>"Shewale", 2=>"PHP", 3=>"Linux");
echo $key = array_search("Rajesh", $name);
?>
Output: 0