PHP: array_search() function
PHP: Searches the array for a given value
The array_search() function is used to search the array against the given value. The function returns the first corresponding key if successful.
Version:
(PHP 4 and above)
Syntax:
array_search(value_search, array_name, strict)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
value_search | Value to search | Required | Mixed* |
array_name | Input array name. | Required | Array |
strict | If TRUE, checks the type of the value. Default value is FALSE | Optional | Boolean |
Return value:
The key for value_search in array_name.
Value Type: Mixed*.
*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.
Example:
<?php
$fruits = array(0 => 'Orange', 1=> 'Apple', 2 => 'Banana',3 => 'Cherry');
$result = array_search('Cherry', $fruits);
echo $result;
?>
Output:
3
Pictorial Presentation:

View the example in the browser
Practice here online :
See also
Previous: array_reverse
Next: array_shift
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises