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' ) ) ) );
Answer from Iván Rodríguez Torres on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
Searches the array for a given value and returns the first corresponding key if successful
🌐
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, ...
Discussions

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
Using php's array_search on an array of objects - Stack Overflow
I like the PHP 7 solution, and will try to remember it when I go to PHP 7. 2016-08-05T18:42:54.017Z+00:00 ... There's no single built-in function that provides for arbitrary comparison. You can, however, roll your own generic array search: More on stackoverflow.com
🌐 stackoverflow.com
Best way to do array_search on multi-dimensional array? - PHP - SitePoint Forums | Web Development & Design Community
So, somehow, I provide “hello” and it returns array(‘key1’, ‘key2’, ‘key3’); - that would be ideal. ... There are a lot of user comments in the PHP docs that can help you with questions such as this, see the below link. More on sitepoint.com
🌐 sitepoint.com
0
May 23, 2012
Given a key, what's the most elegant way to get the previous and next elements in an array?
You have three options: Fetch all keys with array_keys() then array_search() for the key, and -- and ++ in the keys list, to get prev/next. Use reset(), next(), prev(), current(), key() to walk the array and compare the keys until you get to where you want, then use next/prev() to get what you want. Don't use order information in associative arrays. Modify your data structure, so this inefficient and awkward situation doesn't happen. The best option is really 3, but I can't give you concrete advice on the alternative without seeing your data and your needs. But just as one example, if you'll be walking key/values in sequence, you don't need an associative array, you need a list of tuples: $listOfTuples = [['key1', 'value1'], ['key2', 'value2'], ...]; With this, the index is numeric, so walking forward or backward is more intuitive. More on reddit.com
🌐 r/PHP
20
1
May 28, 2017
🌐
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 - The array_search() function searches for a value in an associative array and returns the corresponding key if found, or false if not found.
🌐
Locutus
locutus.io › php › array › array_search
PHP's array_search in JavaScript | Locutus
1 month ago - You you can install via yarn add locutus and require this function via const array_search = require('locutus/php/array/array_search').
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 product)
   {
      if ( $product[$field] === $value )
         return $key;
   }
   return false;
}
Find elsewhere
🌐
Nabilhassen
nabilhassen.com › php-search-multidimensional-array
Searching in a multidimensional array in PHP
October 15, 2025 - Learn all practical ways to search multidimensional arrays in PHP, from simple column lookups to recursive searches for deeply nested structures.
🌐
Medium
medium.com › @rodgersj097 › how-to-search-a-multi-dimensional-array-in-php-with-internal-functions-2d08a5eb4363
How to search a multi-dimensional array in PHP with internal functions. | by Jacob Rodgers | Medium
October 1, 2020 - The way to search a 1d array would be to use the function array_search(‘foo’, $array) but as you may see this doesn't work with a 2d array.
🌐
3D Bay
clouddevs.com › home › php guides › working with php’s array_search() function: a practical guide
Working with PHP's array_search() Function: A Practical Guide
December 11, 2023 - At its core, this function searches for a value within an array and returns the corresponding key if the value is found. If the value is not found, it returns false. ... $strict (optional): A boolean parameter that specifies whether the search ...
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › Array-Functions-array_search
PHP Built-in array_search(), Definition, Syntax, Parameters, Examples | jobtensor
The array_search() function search an array for a value and returns the key. ... <?php // Example 1 $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28); echo array_search(28, $ages); echo "<br>"; // Example 2 $ages2 = array("Mark" => 22, "Jeff" => "22", "Mike" => 28); echo array_search("22", ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_search-function
PHP array_search() Function - GeeksforGeeks
September 12, 2024 - The PHP array_search() function searches an array for a specific value and returns the first corresponding key if found.
🌐
Scaler
scaler.com › home › topics › php array_search() function
PHP array_search() Function - Scaler Topics
March 31, 2024 - The array_search() function in PHP is designed to search for a given value within an array and return the corresponding key if the value is found. This can be incredibly useful in situations where we need to locate a particular element within ...
🌐
Medium
medium.com › @ok4304571 › php-array-search-function-1e665f8c0ed4
PHP array_search() Function - Ok - Medium
July 12, 2025 - The array_search() function is an inbuilt function of PHP. It is used to search the array against the given value. The function returns the first corresponding key if successful.
🌐
Codecademy
codecademy.com › docs › php › arrays › array_search()
PHP | Arrays | array_search() | Codecademy
September 8, 2023 - The array_search() function searches an array for a given value and returns the first matching key for that value.
🌐
ZetCode
zetcode.com › php-array › array-search
PHP array_search - Array Searching in PHP
March 13, 2025 - The PHP array_search function searches an array for a given value. It returns the first corresponding key if the value is found.
🌐
GeeksforGeeks
geeksforgeeks.org › php › search-an-item-in-an-array-in-php
Search an Item in an Array in PHP - GeeksforGeeks
July 23, 2025 - If you want to search for a key in an associative array, array_key_exists() is the right function to use. It checks if the given key or index exists in the array.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-multidimensional-array-search-by-value
PHP multidimensional array search by value - GeeksforGeeks
July 11, 2025 - Check if an element of the given array is itself an array or not and add the element to the search path, else run array search on the nested array. Example: ... <?php // PHP program to carry out multidimensional array search // Function to ...
🌐
SitePoint
sitepoint.com › php
Best way to do array_search on multi-dimensional array? - PHP - SitePoint Forums | Web Development & Design Community
May 23, 2012 - Also i doubt you would be able to find an exact code source for what your wanting as retrieving the index for a specific search is faster then storing each key index. ... function array_find_deep($array, $search, $keys = array()) { foreach($array as $key => $value) { if (is_array($value)) { $sub = array_find_deep($value, $search, array_merge($keys, array($key))); if (count($sub)) { return $sub; } } elseif ($value === $search) { return array_merge($keys, array($key)); } } return array(); } $a = array( 'key1' => array( 'key2' => array( 'key3' => 'value', 'key4' => array( 'key5' => 'value2' ) ) )