PHP array_search() Function

01 Dec, 2021
In this article, we will see how to search the specific value in an array & corresponding return the key using the array_search() function in PHP, & will also understand its implementation through the examples. The array_search() is an inbuilt function in PHP that is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. If there are more than one values then the key of the first matching value will be returned.
Syntax:
array_search($value, $array, strict_parameter)
Parameters: This function takes three parameters as described below:
$value: This is the mandatory field that refers to the value that needs to be searched in the array.
$array: This is the mandatory field that refers to the original array, which needs to be searched.
strict_parameter (optional): This is an optional field that can be set to TRUE or FALSE, and refers to the strictness of search. The default value of this parameter is FALSE. If TRUE, then the function checks for identical elements, i.e., an integer 10 will be treated differently from a string 10.If FALSE, strictness is not maintained.
If TRUE, then the function checks for identical elements, i.e., an integer 10 will be treated differently from a string 10.
If FALSE, strictness is not maintained.
Return Value: The function returns the key of the corresponding value that is passed. If not found then FALSE is returned and if there is more than one match, then the first matched key is returned.
Example: The below program illustrates the array_search() function in PHP.
PHP
<?php   // PHP function to illustrate the use of array_search()  function Search($value, $array)  {      return (array_search($value, $array));  }  $array = array(      "ram",      "aakash",      "saran",      "mohan",      "saran"  );  $value = "saran";  print_r(Search($value, $array));?>
Output:
2
Example: This example illustrates the working of function when the strict_parameter is set to FALSE. Note that the data types of the array and to be searched elements are different. 
PHP
<?php     // PHP function to illustrate the use of array_search()    function Search($value, $array)    {        return (array_search($value, $array, false));    }    $array = array(        45, 5, 1, 22, 22, 10, 10);    $value = "10";    print_r(Search($value, $array));?>
Output:
5
Example: In this example, we will be utilizing the above code to find out what will happen if we pass the strict_parameter as TRUE.
PHP
<?php     // PHP function to illustrate the use of array_search()    function Search($value, $array)    {        return (array_search($value, $array, true));    }    $array = array(45, 5, 1, 22, 22, 10, 10);    $value = "10";    print_r(Search($value, $array));?>
Output:
No Output
Reference: http://php.net/manual/en/function.array-search.php
bhaskargeeksforgeeks
PHP-array
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
How to receive JSON POST with PHP ?
Split a comma delimited string into an array in PHP
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?