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
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
If the third parameter strict is set to true then the array_search() function will search for identical elements in the haystack. This means it will also perform a strict type comparison of the needle in the haystack, and objects must be the ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-multidimensional-array-search-by-value
PHP multidimensional array search by value - GeeksforGeeks
July 11, 2025 - $ --> school3 --> data --> name Multidimensional array search using array_search() method: The array_search() is an inbuilt function which searches for a given value related to the given array column/key.
🌐
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 - Then, we implement the searchForId function. Last, we show how to call the function with different 'uid' values and display the results. This method provides a simple way to search a PHP multidimensional array by value.
🌐
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
🌐
Nabilhassen
nabilhassen.com › php-search-multidimensional-array
Searching in a multidimensional array in PHP
October 15, 2025 - When you need to search by multiple criteria or across multiple levels, a simple loop gives full control. ... This approach is most flexible, you can add complex conditions, nested checks, or partial matches. It’s also easier to debug. array_filter() returns all elements for which the callback returns true. Keys are preserved; use array_values() to reindex if you want a zero-based result array.
🌐
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
<?php gmp_root($base_value, $n_value); ?> 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. This function searches for pattern in subject parameter and replaces them with the replacement.
Find elsewhere
🌐
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) ?
🌐
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.
🌐
IQCode
iqcode.com › code › php › php-search-multidimensional-array-for-multiple-values
php search multidimensional array for multiple values Code Example
function arraySort($input,$sortkey){ foreach ($input as $key=&gt;$val) $output[$val[$sortkey]][]=$val; return $output; } ... /** * PHP Search an Array for multiple key / value pairs */ function multi_array_search($array, $search) { // Create the result array $result = array(); // Iterate over ...
🌐
GitHub
gist.github.com › rseon › 78d6e73b04143b08aaf5147b544499be
[PHP] Search value in multidimensional array · GitHub
Share Copy sharable link for this gist. Clone via HTTPS Clone using the web URL. Learn more about clone URLs · Clone this repository at &lt;script src=&quot;https://gist.github.com/rseon/78d6e73b04143b08aaf5147b544499be.js&quot;&gt;&lt;/script&gt; Save rseon/78d6e73b04143b08aaf5147b544499be to your computer and use it in GitHub Desktop. Download ZIP · [PHP] Search value in multidimensional array ·
🌐
sebhastian
sebhastian.com › php-search-multidimensional-array
PHP how to search multidimensional array with key and value | sebhastian
November 2, 2022 - To handle searching a multidimensional array, you can use either the foreach statement or the array_search() function. A PHP multidimensional array can be searched to see if it has a certain value.
🌐
Talkerscode
talkerscode.com › howto › php-search-multidimensional-array-for-multiple-values.php
PHP Search Multidimensional Array For Multiple Values
In this tutorial we will show you ... here we passing our associative array to the foreach() loop there we using another foreach loop for passing specified key, value pairs to find then we checking key values with array values ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-search-by-multiple-key-value-in-php-array
How to search by multiple key => value in PHP array ? - GeeksforGeeks
July 15, 2024 - To search for elements in a PHP array based on multiple key-value pairs without using foreach, utilize array_filter() with a callback that checks matches using array_intersect_assoc().
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;
}
🌐
ItSolutionstuff
itsolutionstuff.com › post › php-multidimensional-array-search-by-value-exampleexample.html
PHP Multidimensional Array Search By Value Example - ItSolutionstuff.com
May 14, 2024 - If you need to get find value from multidimensional array in php. you can search key value in multidimensional array in php. Here, i will give you simple array what is requirement and how i will solve that problem. right now i have two array $students and $studentsAddress in this example. when i display $students array with foreach loop i also need to display address on those student address too.