Syntax:
The syntax of array_search() function is given below.
The first argument is mandatory and it takes the value that will be searched in the array.
The second argument is mandatory, and it takes the array where the value will be searched.
The third argument is optional, and this argument’s default value is False. If the value of this argument is set to True, then the search value will be matched strictly. That means both the type and value of the search value will require to match with the array value to get the corresponding key value.
The function can return three types of values. It will return an integer or string based on the key value of the array, otherwise returns False if the search value does not exist in the array.
array_search() Function Examples
The uses of the array_search() function have been shown in the next part of this tutorial by using multiple examples.
Example-1: Use of array_search() Without Optional Argument
Create a PHP file with the following script to search the particular number into an array using the array_search() function. The function will return the index position of that number if it exists in the array; otherwise, it will return False. An array of 6 numeric values has been used in the script. The searching number will be taken from the URL parameter.
//Declare a numeric array
$numarr = [80, 67, 34, 50, 12, 39];
//Check the search value is given in the url or not
if(isset($_GET['s']))
{
//Get the search value
$search = $_GET['s'];
//Check the search value exists in the array or not
$output = array_search($search, $numarr);
//Check the returned value of the array_search()
if($output != False)
echo "$search has been found at the position $output.";
else
echo "$search does not exist in the array.";
}
else
echo "No search value is given.";
?>
Output:
The following output will be appeared after executing the above script without any URL parameter.
http://localhost/php/array_search1.php
The following output will be appeared after executing the above script with the URL parameter value 67 that exists in the array at the 2nd position. The index of the array starts from 0. So, the output is 1.
http://localhost/php/array_search1.php?s=67
The following output will be appeared after executing the above script with the URL parameter value, 99, which does not exist in the array.
http://localhost/php/array_search1.php?s=99
Example-2: Use of array_search() With Optional Argument
Create a PHP file with the following script that will search the particular number into an array by using array_search() function with the optional argument value, true. An associative array of 4 values has been used in the script. The searching number will be taken from URL parameter.
//Declare an associative array
$numarr = ["CSE-302"=>2.0,"CSE-407"=>1.0,"CSE-112"=>3.0,"CSE-202"=>2.0];
//Check the search value is given in the url or not
if(isset($_GET['s']))
{
//Get the search value and convert it into the float value
$search = (float) $_GET['s'];
//Check strictly the search value exists in the array or not
$output = array_search($search, $numarr, true);
//Check the returned value of the array_search()
if($output != False)
echo "$search has been found at the key, $output.";
else
echo "$search does not exist in the array.";
}
else
echo "No search value is given.";
?>
Output:
The following output will appear after executing the above script with the URL parameter value, 3 that matches the array value, 3.0.
http://localhost/php/array_search1.php?s=3
The following output will be appeared after executing the above script with the URL parameter value 5.0 that does not exist in the array.
http://localhost/php/array_search1.php?s=5.0
Example-3: Search in Two-Dimensional Array Using array_search()
Create a PHP file with the following script to search the particular value into a two-dimensional array using the array_search() function. An associative two-dimensional array of 3 rows and 3 columns has been used in the script. The searching value will be taken from the URL parameter.
$products = [
['id' => '8976', 'name' => 'HDD', 'price' => '$500'],
['id' => '4590', 'name' => 'Monitor', 'price' => '$600'],
['id' => '1237', 'name' => 'Keyboard', 'price' => '$30']
];
//Check the search value is given in the url or not
if(isset($_GET['s']))
{
//Get the search value
$search = $_GET['s'];
$key = array_search($search, array_column($products, 'name'));
//Check the returned value of the array_search()
if($key != False)
echo "The price of $search is ".$products[$key]['price'].".";
else
echo "$search does not exist in the array.";
}
else
echo "No search value is given.";
?>
Output:
The following output will be appeared after executing the above script with the URL parameter value, ‘Mouse’, that does not match with any value of the name key of the array.
http://localhost/php/array_search1.php?s=Mouse
The following output will be appeared after executing the above script with the URL parameter value, ‘Monitor’ that matches with a value of the name key of the array.
http://localhost/php/array_search1.php?s=Monitor
Example-4: Using array_search() Inside a Function
Create a PHP file with the following script to search the particular value into an array by using the array_search() function inside a user-defined function. An array of 5 string values has been used in the script. Next, the script uses the ternary operator to print the output based on the function’s returned value.
//Declare an array
$languages = ['php', 'perl', 'python', 'java', 'bash'];
//Declare function to search element in the array
function Search_in_array($val, $arr)
{
return array_search($val, $arr, true);
}
//Define the search value
$searchVal = 'perl';
//Call the function
$output = Search_in_array($searchVal, $languages);
//Set message based on the condition
$message = ($output >= 0) ? "$searchVal exists in the array." : "$searchVal does not exist in the array.";
//Print the message
echo $message;
?>
Output:
The following output will be appeared after executing the above script.
Conclusion
Different ways of using the array_search() function for different purposes have been shown in the examples of this tutorial to help the PHP users to know the use of this function properly.