What is difference between in_array and array_search?
Answer Posted / samarth sinha
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("Samarth", "Sinha", "PHP", "Windows");
if (in_array("Samarth", $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=>"Samarth", 1=>"Sinha", 2=>"PHP", 3=>"Windows");
echo $key = array_search("Samarth", $name);
?>
Output: 0
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
How can get all database name using Php and Sql?
What is the best way to make wordpress plugin
How can i know that how many user are visited to my page in php?
What is 301 redirects in php? When are we used 301 redirects in our projects?
What is used of serialize and unserialize in php?
How can export database using php code?
Which framework is best in php among Smarty, Cakephp, Joomla, Drupal, Zend or Something else...? If any then Why?
Is there any way to open notepad within php?
can we pass variable in array to traverse it?
what is main difference between array_push and array_pop?
Given a string=”me,I,myself”; .Using javascript only create an array out of this string and then send a POST request to the url “record.php” without reloading the page. The post key parameters should be n1,n2 and n3 respectively.
How can manage theme in php?
How can restrict user to type upto 8 characters in textbox in php?
Given a string=”me,I,myself”; .Using javascript only create an array out of this string and then send a POST request to the url “record.php” without reloading the page. The post key parameters should be n1,n2 and n3 respectively.
Which keyword is alias for count keyword in php?