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
Syntax
array_find_key(array $array, callable $callback): mixed
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
callback |
fn($value, $key) returns true for a match. |
Return value
The first matching key, or null.
Examples
<?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.
<?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
| Version | Change |
|---|---|
| PHP 8.4 | Added |
In other languages
array.findIndex()· JavaScript
Related
array_find()· Return the first element for which a callback returns true, or null if none match. Added in PHP 8.4.array_search()· Search an array for a value and return the first matching key, or false if it isn't there.- PHP Array Functions · tutorial
- PHP Arrays · tutorial