Example 1 — Finding a Key
Return the key of the first price that is above 5.
<?php
$prices = ['apple' => 3, 'banana' => 8, 'cherry' => 5];
$key = array_find_key($prices, fn($price) => $price > 5);
echo $key, PHP_EOL; How It Works
- The callback is called with 3 first, and 3 is not greater than 5.
- Next it is called with 8, which passes the test.
- array_find_key() returns the key of that item, which is 'banana'.
- The search stops, so 'cherry' is never checked.

