This inbuilt function of PHP 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 first matching value will be returned.
array_search($value, $array, strict_parameter)
This function takes three parameters as described below:
- $value (mandatory): Refers to the value that needs to be searched in the array.
- $array (mandatory): Refers to the original array, which needs to be searched.
- strict_parameter (optional): 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 different from a string 10
- If FALSE, strictness is not maintained.
Return Value: As already mentioned, the function returns the key of the corresponding value that is passed. If not found then FALSE is returned and if there are more than one matches, then the first matched key is returned.
Input : $array = ("ram", "aakash", "saran", "mohan", "aakash") $value = "aakash" Output : 1 Input : $array = (45, 5, 1, 22, 22, 10, 10) $value = 22 Output : 3
Below program illustrates the array_search() function in PHP:
In the below program, we will see how the function works when the strict_parameter is set to FALSE. Note that the data types of the array and to be searched element is different.
Now let’s find out what will happen if we pass the strict_parameter as TRUE, for the same program.
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.