Another possible solution is based on the array_search() function. You must 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
Description
The array_search() function has two arguments. The first is the value you want to fulfill. The second function is a function search. The array_column() function gets the values of elements whose key is 'uid' .
Summary
So you can use it like:
array_search('breville-one-touch-tea-maker-BTM800XL', array_column($products, 'slug'));
or if you want:
// 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');
An initial example (from xfoxawy) can be found on DOCS .
The array_column() page .
Update
Because of Vael's comment, I was curious, so I did a simple test to evaluate the effectiveness of a method that uses array_search and the method suggested in the accepted answer.
I created an array containing 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 did a search test 100 times, looking for different values for the name field, and then calculated the average time in milliseconds . Here you can see an example.
The results were that the method proposed in this answer needed 2E-7 to find the value, while the accepted answer method required about 8E-7.
As I said, both times are quite susceptible to the application using an array with this size. If the size grows, say, 1M elements, then this small difference will be increased.