๐ŸŒ
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.
๐ŸŒ
Laravel News
laravel-news.com โ€บ home โ€บ laravel tutorials โ€บ mongodb vector search in laravel: finding the unqueryable
MongoDB Vector Search in Laravel: Finding the Unqueryable - Laravel News
2 weeks ago - For that, we've added a CLI command you can run (implemented in /app/Console/Commands/CheckVectorIndex.php). That function lists all the indexes for a specific collection and checks for a naming convention in this case. $indexes = iterator_to_array($collection->listSearchIndexes());
Discussions

array_find in PHP 8.4
I've been digging deep into 8.4 territory lately. I'd say array_find is a nice addition; although I do hope we'll get a proper built-in collection class one day. More on reddit.com
๐ŸŒ r/PHP
50
111
July 18, 2024
search - PHP Multidimensional Array Searching (Find key by specific value) - Stack Overflow
I have this multidimensional array. I need to search it and return only the key that matches the value of the "slug". I know there are other threads about searching multidimensional arrays, but I'm... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a PHP equivalent of Javascript's Array.find?

Why don't you make your cities array indexed by id?

More on reddit.com
๐ŸŒ r/PHPhelp
14
5
January 8, 2016
The wrapper for all PHP internal (built-in) array functions and easy array manipulation library on steroids in an object-oriented way.

The wrapper for all PHP internal (built-in) array functions and easy array manipulation library on steroids in an object-oriented way.

Aren't steroids actually bad? :-)

Neat effort, but I need to very clearly point out that reference semantics for objects make very poor value objects and DTOs (which collections typically are used for).

I appreciate this is why the immutable versions exist, but one has to ask themselves how far they want to inconvenience themselves in order to say "my arrays are objects".

For example, I want to question the promise of "easy array manipulation" by asking how do I take immutable array $a and get a copy $b with this manipulation:

$b = $a;
$b['foo']['bar']['baz'] = 123;

I'd like to question the author what is really the big win of typing this:

A::create([1, 2, 3])->foo();

Instead of this:

A::foo([1, 2, 3]);

I realize the above is shorter when you call lots of methods on the resulting object, but that shortness is offset by immense amount of complexity that should compensate for the fact objects don't have pass-by-value semantics as normal arrays do.

You'll also have to round-trip PHP arrays to your arrays and back a lot in practice, because most APIs use PHP arrays.

More on reddit.com
๐ŸŒ r/PHP
14
54
October 5, 2013
People also ask

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
Can I use PHP array_find with associative arrays?
Yes, array_find works with associative arrays and can locate elements by custom rules. Example:
$users = [
    ["id" => 1, "name" => "Ali"],
    ["id" => 2, "name" => "Sara"],
    ["id" => 3, "name" => "Omar"]
];

$user = array_find($users, function($u) {
    return $u["name"] === "Sara";
});
print_r($user); 
// Output: Array ( [id] => 2 [name] => Sara )
๐ŸŒ
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 โ€บ php_arrays_associative.asp
PHP Associative Arrays
For a complete reference of all array functions, go to our complete PHP Array Reference.
๐ŸŒ
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
array_find_key: Returns the key of the first element from the array for which the callback returns true; null otherwise. New PHP 8.4 functions to find whether any or all elements pass a callback check:
๐ŸŒ
Stitcher
stitcher.io โ€บ blog โ€บ array-find-in-php-84
array_find in PHP 8.4 | Stitcher.io
July 18, 2024 - 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
laravel.com โ€บ docs โ€บ 12.x โ€บ strings
Strings - Laravel 12.x - The PHP Framework For Web Artisans
If the string starts with any of the values in the array then that value will be removed from string: ... use Illuminate\Support\Str; $url = Str::chopStart('http://laravel.com', ['https://', 'http://']); // 'laravel.com' The Str::chopEnd method removes the last occurrence of the given value only if the value appears at the end of the string: ... use Illuminate\Support\Str; $url = Str::chopEnd('app/Models/Photograph.php', '.php'); // 'app/Models/Photograph'
Find elsewhere
๐ŸŒ
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 was released in PHP 8.4 to locate a value inside an array and returns the first match.
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-in_array-function
PHP in_array() Function - GeeksforGeeks
June 2, 2025 - The PHP in_array() function is a straightforward and handy tool for checking if a value exists within an array. Its flexibility with the optional strict type checking and compatibility with various data types make it a staple in PHP programming.
๐ŸŒ
PHP
wiki.php.net โ€บ rfc โ€บ array_find
PHP: rfc:array_find
April 7, 2024 - Therefore there is a reason to ... the next PHP version. In addition, the implementation of these functions is quite similar to array_filter and relatively trivial to implement, so the maintenance effort should be low. This RFC proposes to add four new function, array_find, array_find_key, ...
๐ŸŒ
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.
๐ŸŒ
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, ...
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_array_search.asp
W3Schools.com
JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP JS jQuery
Top answer
1 of 10
235

Another poossible solution is based on the array_search() function. You need to use PHP 5.5.0 or higher.

Example

$userdb=Array
(
    0 => Array
        (
            "uid" => '100',
            "name" => 'Sandra Shush',
            "url" => 'urlof100'
        ),

    1 => Array
        (
            "uid" => '5465',
            "name" => 'Stefanie Mcmohn',
            "pic_square" => 'urlof100'
        ),

    2 => Array
        (
            "uid" => '40489',
            "name" => 'Michael',
            "pic_square" => 'urlof40489'
        )
);

$key = array_search(40489, array_column($userdb, 'uid'));

echo ("The key is: ".$key);
//This will output- The key is: 2

Explanation

The function `array_search()` has two arguments. The first one is the value that you want to search. The second is where the function should search. The function `array_column()` gets the values of the elements which key is `'uid'`.

Summary

So you could use it as:
array_search('breville-one-touch-tea-maker-BTM800XL', array_column($products, 'slug'));

or, if you prefer:

// define function
function array_search_multidim($array, $column, $key){
    return (array_search($key, array_column($array, $column)));
}

// use it
array_search_multidim($products, 'slug', 'breville-one-touch-tea-maker-BTM800XL');

The original example(by xfoxawy) can be found on the DOCS.
The array_column() page.


Update

Due to Vael comment I was curious, so I made a simple test to meassure the performance of the method that uses array_search and the method proposed on the accepted answer.

I created an array which contained 1000 arrays, the structure was like this (all data was randomized):

[
      {
            "_id": "57fe684fb22a07039b3f196c",
            "index": 0,
            "guid": "98dd3515-3f1e-4b89-8bb9-103b0d67e613",
            "isActive": true,
            "balance": "$2,372.04",
            "picture": "http://placehold.it/32x32",
            "age": 21,
            "eyeColor": "blue",
            "name": "Green",
            "company": "MIXERS"
      },...
]

I ran the search test 100 times searching for different values for the name field, and then I calculated the mean time in milliseconds. Here you can see an example.

Results were that the method proposed on this answer needed about 2E-7 to find the value, while the accepted answer method needed about 8E-7.

Like I said before both times are pretty aceptable for an application using an array with this size. If the size grows a lot, let's say 1M elements, then this little difference will be increased too.

Update II

I've added a test for the method based in array_walk_recursive which was mentionend on some of the answers here. The result got is the correct one. And if we focus on the performance, its a bit worse than the others examined on the test. In the test, you can see that is about 10 times slower than the method based on array_search. Again, this isn't a very relevant difference for the most of the applications.

Update III

Thanks to @mickmackusa for spotting several limitations on this method:

  • This method will fail on associative keys.
  • This method will only work on indexed subarrays (starting from 0 and have consecutively ascending keys).

Note on Update III

  • not taking performance into account: you can use array_combine with array_keys & array_column to overcome this limitation in a one-liner like:
$product_search_index = 
array_search( 'breville-one-touch-tea-maker-BTM800XL', array_filter( array_combine( array_keys($products), array_column( $products, 'slug' ) ) ) );
2 of 10
176

Very simple:

function myfunction($products, $field, $value)
{
   foreach($products as $key => $product)
   {
      if ( $product[$field] === $value )
         return $key;
   }
   return false;
}
๐ŸŒ
Laravel
laravel.com โ€บ docs โ€บ 12.x โ€บ artisan
Artisan Console - Laravel 12.x - The PHP Framework For Web Artisans
If you need to give the user a predefined set of choices when asking a question, you may use the choice method. You may set the array index of the default value to be returned if no option is chosen by passing the index as the third argument to the method:
๐ŸŒ
GitHub
github.com โ€บ pgvector โ€บ pgvector
GitHub - pgvector/pgvector: Open-source vector similarity search for Postgres
CREATE TABLE items (id bigserial PRIMARY KEY, embedding double precision[]); -- use {} instead of [] for Postgres arrays INSERT INTO items (embedding) VALUES ('{1,2,3}'), ('{4,5,6}');
Starred by 20K users
Forked by 1.1K users
Languages ย  C 77.1% | Perl 22.0%