Jump to content

Please help change my code


johnsmith153

Recommended Posts

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" ) );

Link to comment
Share on other sites

<?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
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.