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


Please Help Members By Posting Answers For Below Questions

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.

2353


Which keyword is alias for count keyword in php?

1673


How can manage theme in php?

2023


What is the best way to make wordpress plugin

2710


What is 301 redirects in php? When are we used 301 redirects in our projects?

1968






what is main difference between array_push and array_pop?

3677


How can get all database name using Php and Sql?

1758


How can restrict user to type upto 8 characters in textbox in php?

1563


How can call any javascript function without saying onclick, onchange, onblur etc events in php?

4158


How can i know that how many user are visited to my page in php?

1609


Is there any way to open notepad within php?

1695


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.

2635


What is used of serialize and unserialize in php?

2567


I am attempting to work on a game panel with multi-server support, so in one of the pages there is a dropdown menu with servers on it, this is the part i am having issues with is server 1 is chosen i need access to the username running a script to server 1 chosen i need a script that will realize its been chosen and pop up below with a username thats associated with it because the users differ on each server so i choose server 1 i need a code to pull from the db and choose the user associated with the said server please help..

2206


Which framework is best in php among Smarty, Cakephp, Joomla, Drupal, Zend or Something else...? If any then Why?

8031