Difference between in_array() and array_search() in php?
Posted by Raj
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", "Sharma", "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=>"Sharma", 2=>"PHP", 3=>"Linux");
echo $key = array_search("Rajesh", $name);
?>
Output: 0
This entry was posted on October 4, 2009 at 12:14 pm, and is filed under
array,
array_search(),
checks,
difference,
echo,
example,
exist,
exit,
how to,
in php,
in_array(),
one,
PHP,
PHP interview questions and answers
.You can leave a response, or trackback from your own site.