Search for values in the elements of an array is an essential operation. Almost every application has such functionality to search for elements from a collective dataset. PHP also provides a facility for its application to search for a particular value from the array. In this tutorial, you will learn about such a function.
PHP array_search() Function
PHP provides a built-in function - array_search(), which is implemented to search for a particular value from an array and return a key. When the value is found, it will return its corresponding key. In case the value is found in the array multiple times, then the function will return the first key matching it.
The syntax of using this function is:
Syntax:
array_search($val, $array, strict_parameter)
Parameters
- Here, you can see that the function takes three different parameters. The first parameter is $val. This is mandatory, and this parameter is used to determine the value that is to be searched in the array.
- The second parameter is the $array, which is another mandatory parameter and is included as a parameter that tells the function which array's value to search for.
- The third parameter, strict_parameter, is an optional one and is either set to TRUE or FALSE, which determines the strictness of the search. By default, the strict_parameter is set to FALSE.
- When strict_parameter is set to TRUE, it will strictly check for identical elements. You can consider it like this; if there is a '10' as a string and another integer value 10, then both will be considered different during the search.
- When strict_parameter is set to FALSE, this strictness of matching and searching is removed.
- In return, this function will provide the key to the corresponding value that is passed. When the value is not found in the array, it will return a FALSE. On the other hand, when there is more than one case matching the target value, then the first matching value's key will be returned.
Example:
Here is a code snippet showing the utilization of this function:
Example:
<?php
// Here is a PHP function that shows the use of array_search()
function Searching($val, $arr)
{
return (array_search($val, $arr));
}
$arr = array(
"Alex",
"Bill",
"Steve",
"Gaurav",
"Neha"
);
$val = "Steve";
print_r(Searching($val, $arr));
?>
Program Output:
2
Another example showing the use of strict_parameter with FALSE value:
Example:
<?php
// Another example showing the FALSE value of Strictness of array_search()
function Searching($val, $arr)
{
return(array_search($val, $arr, false));
}
$array = array(
62,
4,
2,
98,
72,
31,
12
);
$value = "12";
print_r(Searching($val, $arr));
?>
Program Output:
6
Another example showing the use of strict_parameter with TRUE value:
Example:
<?php
// Another example showing the strictness of array_search()
function Searching($val, $arr)
{
return (array_search($val, $arr, true));
}
$array = array(
62,
4,
2,
98,
72,
31,
12
);
$value = "12";
print_r(Searching($val, $arr));
?>
Program Output:
The above program will result in no output because 12 in the array is an integer, and "12" in the value variable is a string.