PHP: check if value and key exist in multidimensional array

Just wondering if you can give me a hand with this:

I have a multidimensional array ...

$my_array = array( 0 => array( "name" => "john", "id" => 4 ), 1 => array( "name" => "mark", "id" => 152 ), 2 => array( "name" => "Eduard", "id" => 152 ) ); 

Any idea of ​​what would be the fastest and most efficient way to check if the array $ my_array contains any value with the key "id" and its value 152. I do not need to echo or use any of the values. I just need to check (return true) if there are entries in the array with the identifier "key" and the value "152".

+53
arrays php
Aug 09 2018-11-11T00:
source share
12 answers

Nothing will be faster than a simple cycle. You can mix and match some array functions, but they will be implemented as a loop.

 function whatever($array, $key, $val) { foreach ($array as $item) if (isset($item[$key]) && $item[$key] == $val) return true; return false; } 
+62
Aug 09 2018-11-11T00:
source share

Here is an updated version of Dan Grossman's answer that will serve multidimensional arrays (what I was after):

 function find_key_value($array, $key, $val) { foreach ($array as $item) { if (is_array($item) && find_key_value($item, $key, $val)) return true; if (isset($item[$key]) && $item[$key] == $val) return true; } return false; } 
+20
Mar 24 '15 at 13:19
source share

** PHP> = 5.5

just u can use this

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

Assume this multidimensional array:

 $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')); 
+9
Jun 21 '16 at 4:02
source share

If you need to do a lot of id searches, and this should be very fast, you should use a second array containing all the ids as keys:

 $lookup_array=array(); foreach($my_array as $arr){ $lookup_array[$arr['id']]=1; } 

Now you can quickly check the existing identifier, for example:

 echo (isset($lookup_array[152]))?'yes':'no'; 
+8
Aug 09 2018-11-11T00:
source share

As in your question, which is actually a simple 2-dimensional array, would it be better? Look -

Let's say your two-dimensional array name is $ my_array and the search value is $ id

 function idExists($needle='', $haystack=array()){ //now go through each internal array foreach ($haystack as $item) { if ($item['id']===$needle) { return true; } } return false; } 

and call him:

 idExists($id, $my_array); 

As you can see, in fact it only checks if there is any internal index with the key "id" with only your value $. Some other answers here may also be true if key_name 'name' also has the value $

+4
Aug 11 '16 at 15:28
source share
 return 0 < count( array_filter( $my_array, function ($a) { return array_key_exists('id', $a) && $a['id'] == 152; } ) ); 

Note: calling array_key_exists is optional. It just codifies endurance.

+2
Aug 09 '11 at 3:13
source share

Try using this code below. It should work great for any search for multidimensional arrays.

Here you can see LIVE DEMO EXAMPLE

 function multi_array_search($search_for, $search_in) { foreach ($search_in as $element) { if ( ($element === $search_for) ){ return true; }elseif(is_array($element)){ $result = multi_array_search($search_for, $element); if($result == true) return true; } } return false; } 
+1
Dec 02 '16 at 16:14
source share

A good solution would be a post provided by @Elias Van Ootegan in a comment:

 $ids = array_column($array, 'id', 'id'); echo isset($ids[40489])?"Exist":"Not Exist"; 

I tried and worked for me, thanks friend.

Edited

Note. It will work in PHP 5.5+

+1
Jan 06 '17 at 6:48
source share

I wrote the following function to determine if a multidimensional array contains some specific value.

 function findKeyValue ($array, $needle, $value, $found = false){ foreach ($array as $key => $item){ // Navigate through the array completely. if (is_array($item)){ $found = $this->findKeyValue($item, $needle, $value, $found); } // If the item is a node, verify if the value of the node contains // the given search parameter. EG: 'value' <=> 'This contains the value' if ( ! empty($key) && $key == $needle && strpos($item, $value) !== false){ return true; } } return $found; } 

Call the function as follows:

 $this->findKeyValue($array, $key, $value); 
0
May 25 '16 at 12:45
source share

Use array_map ()

$ options is the array you are looking for.

 $arr = array_map(function($ar) { if(array_key_exists('name_of_the_key_you_search_for',$ar)){ return true; }else{ return false; } },$options ); var_dump($arr); //just for test, remove in the code 
0
Jul 07 '16 at 12:21
source share
 function checkMultiArrayValue($array) { global $test; foreach ($array as $key => $item) { if(!empty($item) && is_array($item)) { checkMultiArrayValue($item); }else { if($item) $test[$key] = $item; } } return $test; } $multiArray = array( 0 => array( "country" => "", "price" => 4, "discount-price" => 0, ),); $test = checkMultiArrayValue($multiArray); echo "<pre>" print_r($test); 

Will return an array that has an index and a value

0
May 16 '18 at 8:37
source share

The easiest way is as follows:

 $my_array = array( 0 => array( "name" => "john", "id" => 4 ), 1 => array( "name" => "mark", "id" => 152 ), 2 => array( "name" => "Eduard", "id" => 152 ) ); if (array_search(152, array_column($my_array, 'id')) !== FALSE) echo 'FOUND!'; else echo 'NOT FOUND!'; 
0
Dec 12 '18 at 6:01
source share



All Articles