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
This function does that, and returns an array of the appropriate keys to get to said (first) value occurrence. function array_recursive_search_key_map($needle, $haystack) { foreach($haystack as $first_level_key=>$value) { if ($needle === $value) ...
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; }); // []
🌐
Reintech
reintech.io › blog › mastering-php-array-search-function-array-searching-filtering
Mastering PHP's `array_search()` Function for Array Searching and Filtering | Reintech media
April 14, 2023 - If the callback function returns true, the current value from array is returned into the result array. `array_filter()` can be used to filter arrays based on certain conditions. In PHP, the `array_search()` function is used to search for a specific value in an array and return the corresponding key if the value is found...
🌐
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
/** * Checks whether the $callback returns TRUE for ANY of the array * elements. * * @param array $array The array that should be searched. * @param callable $callback The callback function to call to check * each element. The first parameter contains the value ($value), the * second parameter ...
🌐
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, ...
🌐
Stitcher
stitcher.io › blog › array-find-in-php-84
array_find in PHP 8.4 | Stitcher.io
PHP 8.4 adds a handful of functions that have been missing for a while: array_find() and its variants. The purpose of array_find() is simple: pass it an array and a callback, and return the first element for which the callback returns true.
🌐
Laravel News
laravel-news.com › home › new array find functions in php 8.4
New Array Find Functions in PHP 8.4 - Laravel News
June 3, 2024 - Four new array functions are coming ... condition. The new functions are: ... The array_find($array, $callback) function returns the first element for which the $callback returns true:...
🌐
Php-safari
php-safari.com › function › array_search
PHP function - array_search | PHP Safari
<?php array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false · livewire/livewire · <?php } function off($name, $callback) { $index = array_search($callback, $this->listeners[$name] ?? []); $indexAfter = array_search($callback, $this->listenersAfter[$name] ??
🌐
Medium
medium.com › @chriscullis81 › php-8-4-has-new-array-find-functions-bf21252eb0b2
PHP 8.4 has new Array Find Functions | by Chriscullis | Medium
July 10, 2024 - $callback: A callback function that defines the condition. It should return `true` for the element whose index you want to find. ... In this example, `array_find_index()` returns the index (`2`) of the color `blue`. The new array find functions ...
Find elsewhere
🌐
PHP
wiki.php.net › rfc › array_find
PHP: rfc:array_find
The callback function to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns true, the value is returned from array_find and the callback will not be called for further elements.
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php array_filter function
PHP array_filter Function
April 6, 2025 - In this tutorial, you have learned how to use the PHP array_filter() function to filter elements of an array using a callback.
🌐
GitHub
gist.github.com › pbroschwitz › 5956354
array_find.php · GitHub
array_find.php · This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.array-filter.html
Filters elements of an array using a callback function
(PHP 4 >= 4.0.6, PHP 5, PHP 7) array_filter — Filters elements of an array using a callback function · array array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) Iterates over each value in the array passing them to the callback function.
🌐
GitHub
github.com › jasny › php-functions › issues › 5
array_find to find value with callback · Issue #5 · jasny/php-functions
June 22, 2018 - Search or jump to... ... You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... Similar to array_filter, but only return the first value.
Published   Jul 23, 2018
🌐
W3Docs
w3docs.com › php
search a php array for partial string match
This will return the array with matching element(s) You can also use preg_grep() function to search for partial string match · <?php $arr = ['Example1', 'example2', 'exAmple3', 'other']; $result = preg_grep('/example/i', $arr); foreach ($result ...
🌐
ZetCode
zetcode.com › php-array › array-find
PHP array_find - Array Search in PHP
The PHP array_find function searches for an element in an array using a callback function.