Example 1 — Find the Key of a Value
Search a list of fruits and print the index of the match.
<?php
$fruits = ["apple", "banana", "cherry"];
$pos = array_search("banana", $fruits);
echo "Index: ", $pos, PHP_EOL; How It Works
- Create a list of three fruits. Their keys are 0, 1 and 2.
- array_search() scans the list from the start for "banana".
- It finds the match at key 1 and returns that key.
- echo prints the key.

