How to use array_search() Function In PHP
The array_search() function searches for a value in an array and returns the key if found. If the value is not found, it returns false.
Example: The example shows the use of array_search() function.
<?php
$arr = array(10, 20, 30, 40, 50);
$item = 30;
$key = array_search($item, $arr);
if ($key !== false) {
echo "$item exist in array.";
} else {
echo "$item not exist in array.";
}
?>
Output
30 exist in array.
Search an Item in an Array in PHP
Given an array and an item, the task is to search whether the given item is present in the array or not.
Below are the approaches to search an item in an array in PHP:
Table of Content
- Using in_array() Function
- Using array_search() Function
- Using array_key_exists() Function
- Using a Loop