Finding a Key in An Array


The current application I'm working on has multiple "wizards", i.e. multi page forms that store data between the pages, allow moving back and forth, and so on.  These wizards have some things in common, so I have a base controller their individual controllers all extend to access that common functionality, reducing code redundancy and making it more efficient to make changes.

Each wizard utilizes a multi-dimensional array to stores it's individual form values (also arrays).  This makes it much easier to push the necessary data to the forms again for repopulating and for doing the various DB saves on the backend.  It does, however, make some of my base code tricky because certain values might be under different arrays.  

So for WizardA I might have an array like this (note: I'm using generic names here, these are not the real variable names):

Array
(
    [aForm1] => Array
        (
            [name] => Mimi Isme
            [title] => Tester
        )

    [aForm2] => Array
        (
            [phone] => 555-555-5555
            [address] => 123 Any Street, Any Town, TX 77840
            [where_at] => 111
        )

    [aForm3] => Array
        (
            [needComputer] => Yes
            [needEmail] => Yes
        )

)

While wizardB might have:

Array
(
    [aForm1] => Array
        (
            [title] => Tester
            [phone] => 555-555-5555
            [address] => 123 Any Street, Any Town, TX 77840
            [where_at] => 666
        )

    [aForm2] => Array
        (
            [needComputer] => Yes
            [needEmail] => Yes
        )

)

For one of the core functions, I need to find the key 'where_at' and get its value, regardless of the actual array structure.  As far as I could tell going through every array function in PHP, there is nothing native for this.  For searching the value, yes, but not for searching by key.  So I made a little function.  It works, so I'm sharing.

public function findValueOfKey($needle, $aHaystack) {
	$foundValue = NULL;
	
	foreach($aHaystack AS $key => $value):
		if($key == $needle):
			$foundValue = $value;
			break;

		elseif(is_array($value)):
			$foundValue = $this->findValueOfKey($needle, $value);
			
			if(!is_null($foundValue))
				break;
		endif;
	endforeach;
	
	return $foundValue;
}

So if I do findValueOfKey('where_at', $wizardA) I will get 111 while findValueOfKey('where_at', $wizardB) will return 666.  To further illustrate it, here is one more usage using the PHP manual's example of a multi-dimensional array:

$aWizardArray = array(
"foo" => "bar",
	42    => 24,
	"multi" => array(
		"dimensional" => array(
			"array" => "foo"
		)
	)
);

print_r($this->findValueOfKey('array', $aWizardArray) . "<br />");
print_r($this->findValueOfKey('42', $aWizardArray));

Will correctly return:

foo
24

In my usage case case, I know the key will only exist once, I just find the value and done.  If you might have multiple values, then you'd want to tweak it to make found an array, do array_push, and then return the array of found values.  You could also make it even more flexible by adding an option to return first, multiple, or last.  But for my needs, I just need the first (and only) found. 🙂

Now, I'm sure one could argue that I should just redo the arrays so that the keys I need are always exactly the same place, but this is much more flexible and doesn't require me doing excessive twisting of the forms so that they have pointless arrays just to put the variables in the same place, or add extra code to copy the variable where ever it needs to be.  Most importantly, it allows the base to continue to be flexible and usable without having knowledge of the children's functions and configurations, which was my primary goal with it. 🙂