PHP array_find_key() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_find_key() returns the key of the first item in an array for which a callback function returns true. It stops searching as soon as it finds a match.

You use array_find_key() when you need the position or name of an item and not the item itself, for example to know where a record sits before you update or remove it. If nothing matches, it returns null.

Syntax

Call array_find_key() with the values you need. Start with this line.

PHP
$key = array_find_key($prices, fn($p) => $p > 5);
CallWhat it does
array_find_key(['a' => 3, 'b' => 8], fn($v) => $v > 5)Returns 'b', the key of the first value above 5.
array_find_key([5, 6], fn($n) => $n === 5)Returns 0, the integer key of the first item.
array_find_key([1, 3], fn($n) => $n > 10)Returns null, because nothing matches.

array_find_key() was added in PHP 8.4. The callback receives the value first and the key second. If you want the value itself, use array_find().

Library declarations
PHP
array_find_key(array $array, callable $callback): int|string|null

Parameters

ParameterDescription
arrayRequired. The array to search.
callbackRequired. A function that receives the value and the key, like fn($value, $key). It should return true for the item you want.

Return Value

Return ValueDescription
int|stringThe key of the first item for which the callback returned true.
nullNo item matched.

PHP Examples

Example 1 — Finding a Key

Return the key of the first price that is above 5.

PHP
<?php
$prices = ['apple' => 3, 'banana' => 8, 'cherry' => 5];

$key = array_find_key($prices, fn($price) => $price > 5);

echo $key, PHP_EOL;

How It Works

  1. The callback is called with 3 first, and 3 is not greater than 5.
  2. Next it is called with 8, which passes the test.
  3. array_find_key() returns the key of that item, which is 'banana'.
  4. The search stops, so 'cherry' is never checked.

Example 2 — Key 0 and No Match

A match at key 0 is a real result. No match gives null. Compare with === to tell them apart.

PHP
<?php
$first = array_find_key([5, 6], fn($n) => $n === 5);
var_dump($first);

$none = array_find_key([1, 3], fn($n) => $n > 10);
var_dump($none);
var_dump($first === null);

How It Works

  1. The first item, 5, matches, and its key is the integer 0.
  2. var_dump() shows int(0), which is a valid result and not a failure.
  3. No value in the second array is above 10, so the function returns null.
  4. Comparing with === null is false for key 0, so the check is safe.

Example 3 — Locating a Record Before Using It

Find the position of a record, then use the key to read more data from the list.

PHP
<?php
$users = [
    ['id' => 10, 'name' => 'Asha'],
    ['id' => 20, 'name' => 'Ravi'],
    ['id' => 30, 'name' => 'Meena'],
];

$index = array_find_key($users, fn($u) => $u['name'] === 'Ravi');

var_dump($index);
echo $users[$index]['id'], PHP_EOL;

How It Works

  1. The callback compares the 'name' field of each record with 'Ravi'.
  2. Ravi is the second record, so array_find_key() returns the key 1.
  3. $users[$index] uses that key to read the whole record again.
  4. The id of the record, 20, is printed.

Frequently Asked Questions

It returns the key of the first item for which a callback returns true. If no item matches, it returns null.
array_find_key() was added in PHP 8.4. It is not available in older versions.
array_search() looks for a plain value. array_find_key() runs your callback, so it can test any condition, and it passes both the value and the key to the callback.
The key can be 0, which is falsy in PHP. A plain if check would treat a match at key 0 as no match. Comparing with === null avoids this mistake.
Use array_find(), which returns the first matching value. You can also read the value with $array[$key] after you have the key.

Next: array_first()

array_first() returns the first value of an array, no matter what its key is. If the array.

array_first() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful