To pull the first one from the array, or return false:

current(array_filter($myArray, function($element) { ... }))

More info on current() here.

Answer from Izkata on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
About searching in multi-dimentional arrays using array_column, to add to the notes from "turabgarip at gmail dot com", here is a workaround to find the correct key: $array_column = 'name'; $searchValue = 'my value ; $result = array_filter($myArray, function ($subarray) use ($array_column, $searchValue) { return isset($subarray[$array_column]) && $subarray[$array_column] === $searchValue; }); echo key($result);
Top answer
1 of 7
77

To pull the first one from the array, or return false:

current(array_filter($myArray, function($element) { ... }))

More info on current() here.

2 of 7
69

Here's a basic solution

function array_find(f) {
  foreach (x) {
    if (call_user_func(x) === true)
      return $x;
  }
  return null;
}

array_find([1,2,3,4,5,6], function($x) { return $x > 4; });  // 5
array_find([1,2,3,4,5,6], function($x) { return $x > 10; }); // null

In the event x) returns true, the loop short circuits and $x is immediately returned. Compared to array_filter, this is better for our use case because array_find does not have to continue iterating after the first positive match has been found.

In the event the callback never returns true, a value of null is returned.


Note, I used call_user_func(x) instead of just calling x). This is appropriate here because it allows you to use any compatible callable

Class Foo {
  static private $data = 'z';
  static public function match($x) {
    return $x === self::$data;
  }
}

array_find(['x', 'y', 'z', 1, 2, 3], ['Foo', 'match']); // 'z'

Of course it works for more complex data structures too

$data = [
  (object) ['id' => 1, 'value' => 'x'],
  (object) ['id' => 2, 'value' => 'y'],
  (object) ['id' => 3, 'value' => 'z']
];

array_find($data, function($x) { return $x->id === 3; });
// stdClass Object (
//     [id] => 3
//     [value] => z
// )

If you're using PHP 7, add some type hints

function array_find(array $xs, callable $f) { ...

If your array may contain null elements, array_find cannot return null to signal no element was not found. As @dossy suggests, you could use an array result containing either one or zero elements -

function array_find(f) {
  foreach (x) {
    if (call_user_func(x) === true)
      return [$x]; // result
  }
  return []; // not found
}

array_find([1,2,3,4,5,6], function($x) { return $x > 4; });  // [5]
array_find([1,2,3,4,5,6], function($x) { return $x > 10; }); // []
🌐
PHP
wiki.php.net › rfc › array_find
PHP: rfc:array_find
If this function returns true, the key is returned from array_find_key and the callback will not be called for further elements. The function returns the key of the first element for which the $callback returns true. If no matching element is found the function returns NULL. $array = [ 'a' => 'dog', 'b' => 'cat', 'c' => 'cow', 'd' => 'duck', 'e' => 'goose', 'f' => 'elephant' ]; // Find the first animal with a name longer than 4 characters.
People also ask

What does PHP array_find do?
The array_find function searches through an array and returns the first element that matches a condition. Example:
$result = array_find([1, 2, 3, 4], function($n) {
    return $n > 2;
});
echo $result; // Output: 3
🌐
flatcoding.com
flatcoding.com › home › php array_find: how to locate array values with examples
PHP array_find: How to Locate Array Values with Examples - FlatCoding
How is PHP array_find different from array_filter?
  • array_find returns the first matching value.
  • array_filter returns all matching values.
Example:
$array = [1, 2, 3, 4, 5];

$find = array_find($array, fn($n) => $n > 3);
echo $find; // Output: 4

$filter = array_filter($array, fn($n) => $n > 3);
print_r($filter); // Output: [4, 5]
🌐
flatcoding.com
flatcoding.com › home › php array_find: how to locate array values with examples
PHP array_find: How to Locate Array Values with Examples - FlatCoding
How to write a custom PHP array_find function?
You can create a reusable array_find function that works with any type of array. Example:
function array_find($array, $callback) {
    foreach ($array as $key => $value) {
        if ($callback($value, $key)) {
            return $value;
        }
    }
    return null;
}

$numbers = [10, 20, 30, 40];
$result = array_find($numbers, fn($n) => $n === 30);
echo $result; // Output: 30
🌐
flatcoding.com
flatcoding.com › home › php array_find: how to locate array values with examples
PHP array_find: How to Locate Array Values with Examples - FlatCoding
🌐
W3Schools
w3schools.com › php › func_array_search.asp
PHP array_search() Function
The array_search() function search an array for a value and returns the key. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, ...
🌐
GitHub
github.com › PHP-Polyfills › array-find
GitHub - PHP-Polyfills/array-find: PHP: Provides a user-land polyfill for `array_find`, `array_find_key`, `array_any` and `array_all` functions added in PHP 8.4.
Provides user-land PHP polyfills for the array_find, array_find_key, array_any and array_all functions added in PHP 8.4. Requires PHP 7.1 or later.
Author   PHP-Polyfills
🌐
FlatCoding
flatcoding.com › home › php array_find: how to locate array values with examples
PHP array_find: How to Locate Array Values with Examples - FlatCoding
August 27, 2025 - PHP array_find is a simple way to locate array values. Click here to see examples and learn syntax with clear steps.
🌐
Reddit
reddit.com › r/php › array_find in php 8.4
r/PHP on Reddit: array_find in PHP 8.4
July 18, 2024 - Still those prehistorical functions... You should be able to do $array->find(); ... For real. I found this quite a few years ago and been wanting support for it built in PHP for years.
🌐
Medium
medium.com › @valerio_27709 › how-to-search-in-a-php-associative-array-fast-tips-5890cdf818e0
How to Search in a PHP Associative Array — Fast tips | by Valerio Barbera | Medium
August 15, 2024 - The array_key_exists() function checks if a specific key exists in an associative array. It returns `true` if the key is found and `false` otherwise. $fruits = [ 'apple' => 'red', 'banana' => 'yellow', ]; if (array_key_exists('banana', $fruits)) ...
Find elsewhere
🌐
PHP.Watch
php.watch › codex › array_find
array_find Function • PHP.Watch
PHP 7 · PHP 8.0-8.1 · PHP 8.2-8.3 · PHP 8.4 · Added · PHP 8.5 · PHP 8.6 · array_find(array $array, callable $callback): mixed · Typearray · Typecallable · The callback function to call to check each element, which must be · If this function returns `true`, the value is returned from ...
🌐
Reddit
reddit.com › r/phphelp › is there a php equivalent of javascript's array.find?
r/PHPhelp on Reddit: Is there a PHP equivalent of Javascript's Array.find?
January 8, 2016 -

array_filter gets you all items that match a given condition, but what I want is to get the first such item

Let's say I want to display a link to a country's capital, given an ID number and a query of the country's cities.

I could do something like

echo $country["Capital"] ?
cityToLink(
	array_filter($cities,
	function($city){
		global $country;
		return $city["ID"] == $country["Capital"];
	}
)[0]):
"N/A"

but that falls apart if the index of the needed city isn't 0, since it seems that array_filter keeps the original indexes intact

🌐
ZetCode
zetcode.com › php-array › array-find
PHP array_find - Array Search in PHP
<?php declare(strict_types=1); function array_find(array $array, callable $callback): mixed { foreach ($array as $element) { if ($callback($element)) { return $element; } } return null; } $numbers = [1, 3, 4, 7, 8]; $firstEven = array_find($numbers, fn($n): bool => $n % 2 === 0); echo $firstEven ??
🌐
Stitcher
stitcher.io › blog › array-find-in-php-84
array_find in PHP 8.4 | Stitcher.io
The decision for array_find() over array_first() isn't all that weird though: lots of languages implement a method to find the first matching element from an array, and those functions are always called find.
🌐
PHP.Watch
php.watch › versions › 8.4 › array_find-array_find_key-array_any-array_all
New `array_find`, `array_find_key`, `array_any`, and `array_all` functions - PHP 8.4 • PHP.Watch
If * this function returns TRUE, the key ($key) is returned * immediately and the callback will not be called for further * elements. * * @return mixed The key of the first element for which the * $callback returns TRUE. NULL, If no matching element is found. */ function array_find_key(array $array, callable $callback): mixed {}
🌐
DEV Community
dev.to › gbhorwood › php-write-php-84s-arrayfind-from-scratch-5c9m
php: write php 8.4’s array_find from scratch - DEV Community
July 9, 2024 - the only difference between array_find_key and array_find is that the one that is called ‘find_key’ finds the key, not the value.
🌐
Reddit
reddit.com › r/php › php: rfc:array_find
r/PHP on Reddit: PHP: rfc:array_find
April 21, 2024 - PHP is already criticized for inconsistent naming and these functions will add to it. I cannot think a better name for it though. array_usearch would allude to both existing function and the u-something convention that denotes a function that uses callback. But I don't like it either... May be a pair array_ufind()/array_ufind_get_key() would do. Edit: or, well, for those familiar with javascript's array.find(), array_find()/array_find_get_key(), though I still maintain that find being a synonym for search woud inevitably create a confusion.