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-find.php
PHP: array_find - Manual
array_find() returns the value of the first element of an array for which the given callback returns true.
๐ŸŒ
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, ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ search-an-item-in-an-array-in-php
Search an Item in an Array in PHP - GeeksforGeeks
July 23, 2025 - The loop iterates through the array and compares each element with the search value 30. If a match is found, it prints the index and breaks the loop. Example: This example shows the use of loop for searching an element in an array. ... <?php $arr = array(10, 20, 30, 40, 50); $item = 30; $found = false; foreach ($arr as $key => $value) { if ($value == $item) { echo "$item Exist in Array at Index $key."; $found = true; break; } } if (!$found) { echo "$item not Exist in Array."; } ?>
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-find-the-index-of-an-element-in-an-array-using-php
How to find the index of an element in an array using PHP ? - GeeksforGeeks
August 26, 2024 - Using a loop to find the index of an element involves iterating through the array with a foreach loop. Check each value, and if it matches the target element, store the current key as the index and exit the loop.
๐ŸŒ
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.
๐ŸŒ
Code.mu
code.mu โ€บ en โ€บ php โ€บ manual โ€บ array โ€บ array_search
The array_search Function - Array Search in PHP
Let's find an element with the value 'c' in the array - as a result, we will get its key (it is equal to 2): <?php $arr = ['a', 'b', 'c', 'd', 'e']; echo array_search('c', $arr); ?>
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; }); // []
๐ŸŒ
Inspector
inspector.dev โ€บ home โ€บ how to search in a php associative array โ€“ fast tips
How to Search in a PHP Associative Array - inspector.dev
July 1, 2025 - In this tutorial, we will explore various methods and techniques to search for values in a PHP associative array. For more technical articles you can follow me on Linkedin or X.
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ php-find-value-in-an-array
PHP find value in an array - javatpoint
It is a server-side scripting language and a powerful tool for creating a dynamic and interactive website. PHP is an interpreted language, so it doesn't need compilation. It is specially designed... ... In this section, we are going to learn multidimensional array search by using the value.
๐ŸŒ
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 none of the elements returned true, the array_find function returns null. /** * Returns the VALUE of the first element from $array for which the * $callback returns true. Returns NULL if no matching element is * found. * * @param array $array The array that should be searched.
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ array_find in php 8.4
r/PHP on Reddit: array_find in PHP 8.4
July 18, 2024 - You can see from experience what to include, exclude, and add to that feature based on its history and what went well with it or didn't.. ... Love the simple additions! ... 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.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_array_in_array.asp
PHP in_array() Function
connection_aborted() connection_status() connection_timeout() constant() define() defined() die() eval() exit() get_browser() __halt_compiler() highlight_file() highlight_string() hrtime() ignore_user_abort() pack() php_strip_whitespace() show_source() sleep() sys_getloadavg() time_nanosleep() time_sleep_until() uniqid() unpack() usleep() PHP MySQLi ยท affected_rows autocommit change_user character_set_name close commit connect connect_errno connect_error data_seek debug dump_debug_info errno error error_list fetch_all fetch_array fetch_assoc fetch_field fetch_field_direct fetch_fields fetch_l
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ php โ€บ php_function_array_search.htm
PHP - Function array_search()
<?php $input = array("a"=>"banana","b"=>"apple","c"=>"Mango"); print_r(array_search("apple", $input)); ?>
๐ŸŒ
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.
๐ŸŒ
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 - 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. It can perform either strict or loose comparison, depending on the optional third parameter.