Search for multidimensional PHP arrays (search by a specific value)

I have this multidimensional array. I need to find it and return only the key that matches the value of "slug". I know that there are other topics related to the search for multidimensional arrays, but I am not well versed in my situation. Thanks so much for any help!

I need a function like:

myfunction($products,'breville-one-touch-tea-maker-BTM800XL'); // returns 1 

Here's the array:

 $products = array ( 1 => array( 'name' => 'The Breville One-Touch Tea Maker', 'slug' => 'breville-one-touch-tea-maker-BTM800XL', 'shortname' => 'The One-Touch Tea Maker', 'listprice' => '299.99', 'price' => '249.99', 'rating' => '9.5', 'reviews' => '81', 'buyurl' => 'http://www.amazon.com/The-Breville-One-Touch-Tea-Maker/dp/B003LNOPSG', 'videoref1' => 'xNb-FOTJY1c', 'videoref2' => 'WAyk-O2B6F8', 'image' => '812BpgHhjBML.jpg', 'related1' => '2', 'related2' => '3', 'related3' => '4', 'bestbuy' => '1', 'quote' => '', 'quoteautor' => 'K. Martino', ), 2 => array( 'name' => 'Breville Variable-Temperature Kettle BKE820XL', 'slug' => 'breville-variable-temperature-kettle-BKE820XL', 'shortname' => 'Variable Temperature Kettle', 'listprice' => '199.99', 'price' => '129.99', 'rating' => '9', 'reviews' => '78', 'buyurl' => 'http://www.amazon.com/Breville-BKE820XL-Variable-Temperature-1-8-Liter-Kettle/dp/B001DYERBK', 'videoref1' => 'oyZWBD83xeE', 'image' => '41y2B8jSKmwL.jpg', 'related1' => '3', 'related2' => '4', 'related3' => '5', 'bestbuy' => '1', 'quote' => '', 'quoteautor' => '', ), ); 
+61
php search multidimensional-array key
Nov 12 '11 at 3:01
source share
6 answers

Very simple:

 function myfunction($products, $field, $value) { foreach($products as $key => $product) { if ( $product[$field] === $value ) return $key; } return false; } 
+102
Nov 12 '11 at 3:05
source share

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.

+60
Jan 14 '16 at 9:11
source share

This class method can search in an array for several conditions:

 class Stdlib_Array { public static function multiSearch(array $array, array $pairs) { $found = array(); foreach ($array as $aKey => $aVal) { $coincidences = 0; foreach ($pairs as $pKey => $pVal) { if (array_key_exists($pKey, $aVal) && $aVal[$pKey] == $pVal) { $coincidences++; } } if ($coincidences == count($pairs)) { $found[$aKey] = $aVal; } } return $found; } } // Example: $data = array( array('foo' => 'test4', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test1', 'bar' => 'baz3'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz4'), array('foo' => 'test4', 'bar' => 'baz1'), array('foo' => 'test', 'bar' => 'baz1'), array('foo' => 'test3', 'bar' => 'baz2'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test', 'bar' => 'baz'), array('foo' => 'test4', 'bar' => 'baz1') ); $result = Stdlib_Array::multiSearch($data, array('foo' => 'test4', 'bar' => 'baz1')); var_dump($result); 

Will produce:

 array(2) { [5]=> array(2) { ["foo"]=> string(5) "test4" ["bar"]=> string(4) "baz1" } [10]=> array(2) { ["foo"]=> string(5) "test4" ["bar"]=> string(4) "baz1" } } 
+11
Jan 23 '14 at 19:00
source share

I found a difficult situation with the first answer (sorry, I do not have enough reputation to comment on this).

When you find the desired value, and associated with it - 0. If after that you use conditional operators, not only check the return value, but also enter (identical).

 $key = myfunction($products, $field, $value); // Good if($key !== FALSE){ } if($key === FALSE){ } // Not Good if($key != FALSE){ } if($key == FALSE){ } 
+5
Mar 04 '15 at 16:37
source share
 function search($array, $key, $value) { $results = array(); if (is_array($array)) { if (isset($array[$key]) && $array[$key] == $value) $results[] = $array; foreach ($array as $subarray) $results = array_merge($results, search($subarray, $key, $value)); } return $results; } 
0
Jan 14 '15 at 14:34
source share

try it

 function recursive_array_search($needle,$haystack) { foreach($haystack as $key=>$value) { $current_key=$key; if($needle==$value['uid'] OR (is_array($value) && recursive_array_search($needle,$value) !== false)) { return $current_key; } } return false; } 
-one
Oct 07 '16 at 6:07
source share



All Articles