johnsmith153 Posted May 23, 2012 Share Posted May 23, 2012 The code below will return a value from a multi-dimensional array. I need to do the opposite of what the code does by providing a value and returning the keys, e.g. it would return array('key1', 'key2', 'key3', 'key4'); etc. - can anyone help? function GetArrKey( $findArr, $key_arr, $depth=0 ) { if( count($key_arr) <= $depth || !array_key_exists($key_arr[$depth], $findArr) ) return NULL; else if( count($key_arr) == $depth+1 ) return $findArr[$key_arr[$depth]]; return GetArrKey( $findArr[$key_arr[$depth]], $key_arr, $depth+1 ); } $value = GetArrKey( $array, array( "key1", "key2", "key3", "key4" ) ); Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 23, 2012 Share Posted May 23, 2012 How do you want it to behave if the value exists at multiple "locations" in the multidimensional array? Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 23, 2012 Share Posted May 23, 2012 <?php $myArr = array(); $myArr['test']['testing'][1] = "dont find me"; $myArr[1][2][3] = "test"; $myArr['one']['two'][1] = "foo"; $myArr['one']['two'][2] = "bar"; function getArrKeys($arr, $value, $existingKeys=NULL){ if(!$existingKeys){ $existingKeys = array(); } if(is_array($arr)){ $key = array_search($value, $arr); if($key !== FALSE){ $existingKeys[] = $key; }else{ foreach($arr AS $k=>$a){ $key = getArrKeys($a, $value, $existingKeys); if($key){ $existingKeys[] = $k; $existingKeys = array_merge($existingKeys, $key); return $existingKeys; } } } if(count($existingKeys)){ return $existingKeys; }else{ return FALSE; } }else{ return FALSE; } } print '<pre>'; print "Foo "; print_r(getArrKeys($myArr, "foo")); print "Bar "; print_r(getArrKeys($myArr, "bar")); print "find me "; print_r(getArrKeys($myArr, "find me")); print "test "; print_r(getArrKeys($myArr, "test")); ?> Results: Foo Array ( [0] => one [1] => two [2] => 1 ) Bar Array ( [0] => one [1] => two [2] => 2 ) find me test Array ( [0] => 1 [1] => 2 [2] => 3 ) Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted May 23, 2012 Author Share Posted May 23, 2012 Hi, Just seen this and I'll give it a test. As to your question, it shouldn't exist at multiple locations (if the values are correct). I'll look at your code and see if you've included something to deal with that. Maybe another multi-dimensional array could be returned?? Thanks for taking the time to look at this. Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 23, 2012 Share Posted May 23, 2012 I didn't, this will only find the first instance of it. You'd have to switch to array_keys as per the docs on array_search Quote Link to comment Share on other sites More sharing options...
johnsmith153 Posted May 23, 2012 Author Share Posted May 23, 2012 Works perfectly. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.