PHP 8 reference · Array Functions

PHP array_find_key() Function

Return the key of the first element for which a callback returns true, or null if none match. Added in PHP 8.4.

PHP 8 Since 8.4

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_find_key(array $array, callable $callback): mixed

Parameters

ParameterDescription
array The input array.
callback fn($value, $key) returns true for a match.

Return value

The first matching key, or null.

Examples

Which item is out of stock?
<?php
$stock = ["tea" => 12, "cake" => 0, "milk" => 4];
echo array_find_key($stock, fn($qty) => $qty === 0), "\n";

Output

cake

Run this on your computer — see “Get Started” for setup.

No match
<?php
var_dump(array_find_key(["x", "y"], fn($v) => $v === "z"));

Output

NULL

Run this on your computer — see “Get Started” for setup.

Version history

VersionChange
PHP 8.4Added

In other languages

Was this page helpful?