Since PHP 5.5, you can use array_column:

$ids = array_column($users, 'id');

This is the preferred option on any modern project. However, if you must support PHP<5.5, the following alternatives exist:

Since PHP 5.3, you can use array_map with an anonymous function, like this:

$ids = array_map(function ($ar) {return $ar['id'];}, $users);

Before (Technically PHP 4.0.6+), you must create an anonymous function with create_function instead:

$ids = array_map(create_function('$ar', 'return $ar["id"];'), $users);
Answer from phihag on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.array-values.php
PHP: array_values - Manual
This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x <?php /** * Get all values from specific key in a multidimensional array * * @param $key string * @param $arr array * @return null|string|array */ function array_value_recursive($key, array $arr){ $val = array(); array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v); }); return count($val) > 1 ?
๐ŸŒ
GitHub
gist.github.com โ€บ 53a9b4e85cfc460eb6c22346ceb68f01
Get all values from specific key in a multidimensional array, similar ...
Get all values from specific key in a multidimensional array, similar to array_columns - array-value-recursive.php
๐ŸŒ
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)) ...
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;
}
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ how to find the position of a key in a multidimensional array?
r/PHPhelp on Reddit: How To Find The Position Of A Key In A Multidimensional Array?
July 1, 2023 -

I am querying data from an API and the return result is very cumbersome to deal with.

It's a multidimensional array but the indexes are a mix of keys and ints. Like

$result[0]['keyValueX'][0]['keyValueY']

And it's possible that using another type of query will yield the result in a different order of indexes:

$result['keyValueB'][0]['keyValueX']

But what I'm interested in, is knowing if the result has a particular key that exists somewhere deep within it. At any level. And if it does exist, just return the value that is associated with that key......instead of me having to type out all these indexes just to reach that place.

What's the way to do this? I have tried

array_keys($result)

but when I do that, it only gives me the first level of keys that exist. On the outermost level. It does not go deeper in.

๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ how-to-get-specific-key-value-from-multidimensional-array-in-phpexample.html
How to Get Specific Key Value from Multidimensional Array PHP? - ItSolutionstuff.com
May 14, 2024 - we almost require to get specific key and value in array when work with php multidimensional array. like if you have one multidimensional array with each array with id, name, email etc key. You need to get only all name from array then how you can get it?, i will show you how we can do it.
๐ŸŒ
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:
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ Learning-PHP-Is-there-a-way-to-get-the-value-of-multi-dimensional-array-by-specifying-the-key-with-a-variable
Learning PHP: Is there a way to get the value of multi-dimensional array by specifying the key with a variable? - Quora
Answer (1 of 14): [code]$key = '["a"]["b"]'; echo $array{$key} [/code]will not and should not work. This will work, but Iโ€™m not clear on exactly what youโ€™re trying to do, so this may not be the best answer either: [code]$one = $_REQUEST["user_input_value_for_a"]; $two = $_REQUEST["user_input_va...
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_arrays_multidimensional.asp
PHP Multidimensional Arrays
MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data ยท PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser ยท AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll ยท PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() array_chunk() array_column() array_combine() array_co
๐ŸŒ
Quora
quora.com โ€บ How-do-I-set-a-key-in-a-multidimensional-array-in-PHP
How to set a key in a multidimensional array in PHP - Quora
Answer (1 of 4): You should know the full path until the dimension where you want to add a new key, then you can directly set the value. See the example below. [code]$product = array( 'type' => 'electronics', 'brand' => 'sony', 'details' => array( 'model' => 'WS283', 'power' => array(...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ multidimensional-associative-array-in-php
Multidimensional Associative Array in PHP - GeeksforGeeks
July 12, 2025 - The last key i.e. description of each parent key has been associated with another array of the set of keys and constant values. Here Python and PHP are parent key for first_release, latest_release, designed_by and description whereas description is parent key for the extension, typing_discipline, and license. Retrieving Values: We can retrieve the value of multidimensional array using the following method:
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-fetch-the-key-value-from-a-multidimensional-array
How to fetch the key value from a multidimensional array - Quora
Answer (1 of 2): In what language ? My example in PHP [code] [/code]You can apply same algorithm in other languages too. Yes, t...