function searchForId($id, $array) {
   foreach ($array as $key => $val) {
       if ($val['uid'] === $id) {
           return $key;
       }
   }
   return null;
}

This will work. You should call it like this:

$id = searchForId('100', $userdb);

It is important to know that if you are using === operator compared types have to be exactly same, in this example you have to search string or just use == instead ===.

Based on angoru answer. In later versions of PHP (>= 5.5.0) you can use one-liner.

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

Here is documentation: http://php.net/manual/en/function.array-column.php.

Answer from Jakub Truneček on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-multidimensional-array-search-by-value
PHP multidimensional array search by value - GeeksforGeeks
July 11, 2025 - <?php // PHP program to carry out multidimensional array search // Function to iteratively search for a given value function searchForId($search_value, $array, $id_path) { // Iterating over main array foreach ($array as $key1 => $val1) { $temp_path ...
🌐
Uptimia
uptimia.com › home › questions › how to search a php multidimensional array by value?
How To Search A PHP Multidimensional Array By Value?
November 5, 2024 - Two useful functions are array_column() and array_search(). array_column() takes a column from a multidimensional array. It needs three inputs: the array, the column key to extract, and an optional index key.
🌐
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 - array_search(0235, array_column($2dArray, 'Vin')) PHP · Multidimensional · Arrays · 1 follower · ·1 following · Help · Status · About · Careers · Press · Blog · Privacy · Rules ·
🌐
TutorialsPoint
tutorialspoint.com › how-to-search-by-key-value-in-a-multidimensional-array-in-php
How to Search by key=value in a Multidimensional Array in PHP
Here's an example of using array_filter() and array_column() to search for a key-value pair in a multidimensional array in PHP:
🌐
Javatpoint
javatpoint.com › php-multidimensional-array-search-by-value
PHP Multidimensional Array Search By Value - javatpoint
Syntax The syntax shows the GMP function to get a root value of the base parameter. &lt;?php gmp_root($base_value, $n_value); ?&gt; Parameters The function takes two variables: a $base value and... ... PHP The is a built-in function of PHP. It is used to perform a regular expression search and replace.
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
array_search — Searches the array for a given value and returns the first corresponding key if successful
Find elsewhere
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;
}
🌐
Expertrec
blog.expertrec.com › expertrec custom search engine › others › how to search for a value in a multidimensional array using php - expertrec
How to Search for a Value in a Multidimensional Array using PHP - Expertrec
PHP search value in the multidimensional array can be done by the array_search() which is an in-built function that searches for a particular value associated with the given array column or key.
Published   July 14, 2025
🌐
Clue Mediator
cluemediator.com › multidimensional-array-search-by-value-in-php
Multidimensional array search by value in PHP - Clue Mediator
array_search($value['id'], array_column($employeesData, 'emp_id')); Let’s use the following code to get the output. <!--?php foreach($employees as $key =--> $value){ $index = array_search($value['id'], array_column($employeesData, 'emp_id')); $email = ($index!== false) ?
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-search-by-keyvalue-in-a-multidimensional-array-in-php
How to search by key=>value in a multidimensional array in PHP ? - GeeksforGeeks
July 15, 2024 - ... <?php // PHP program to carry out multidimensional array // search by key=>value // Function to recursively search for a // given key=>value function search($array, $key, $value) { $results = array(); // if it is array if (is_array($array)) ...
🌐
GitHub
gist.github.com › rseon › 78d6e73b04143b08aaf5147b544499be
[PHP] Search value in multidimensional array · GitHub
[PHP] Search value in multidimensional array · Raw · get_by_key_value.php · This file contains hidden or 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.
🌐
WP-Mix
wp-mix.com › php-search-multidimensional-array
PHP Search Multidimensional Array | WP-Mix
function shapeSpace_search_array($needle, $haystack) { if (in_array($needle, $haystack)) { return true; } foreach ($haystack as $item) { if (is_array($item) && array_search($needle, $item)) return true; } return false; } This function returns true if the specified value ($needle) is found within the specified array ($haystack), or false if the value is not found in the array. Works on any array, flat/one-dimensional arrays and multidimensional arrays.
🌐
sebhastian
sebhastian.com › php-search-multidimensional-array
PHP how to search multidimensional array with key and value | sebhastian
November 2, 2022 - Suppose you have a multidimensional ... => "Michael", "age" => 30, ], ]; To search the array by its value, you can use the foreach statement....
🌐
Koding Made Simple
kodingmadesimple.com › 2015 › 12 › search-multidimensional-array-for-key-value-php.html
Search Multidimensional Array for Key Value in PHP
We have to do it manually and here is the PHP function to search for the nested array values and return the key from the multidimensional array.
🌐
GitHub
gist.github.com › raazon › b39d782e4908b6bf5f359c96ff7b8839
PHP multidimensional array search · GitHub
PHP multidimensional array search · Raw · php-multidimensional-array-search.php · This file contains hidden or 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.
🌐
SitePoint
sitepoint.com › php
Best way to do array_search on multi-dimensional array? - PHP - SitePoint Forums | Web Development & Design Community
May 23, 2012 - 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 ...