1

The other day I asked a question related to this, and I got an answer, but it did not do what I wanted. Here is the method I have for traversing a multidimensional associative array, checking whether a key is in the array (from the answer to my previous question):

private function checkKeyIsInArray($dataItemName, $array)
{
    foreach ($array as $key => $value)
    {
        // convert $key to string to prevent key type convertion
        echo '<pre>The key: '.(string) $key.'</pre>';

        if ((string)$key == $dataItemName)
            return true;

        if (is_array($value))
            return $this->checkKeyIsInArray($dataItemName, $value);

    }
    return false;
}

Here is my array stucture:

Array (
    [0] => Array ( [reset_time] => 2013-12-11 22:24:25 )
    [1] => Array ( [email] => someone@example.com )
)

The method traverses the first array branch, but not the second. Could someone explain why this might be the case please? It seems I am missing something.

Prix
  • 19,417
  • 15
  • 73
  • 132
Steve Cooke
  • 447
  • 1
  • 4
  • 14

1 Answers1

4

The problem is that you return whatever the recursive call returns, regardless of whether it succeeded or failed. You should only return if the key was found during the recursion, otherwise you should keep looping.

private function checkKeyIsInArray($dataItemName, $array)
{
    foreach ($array as $key => $value)
        {
            // convert $key to string to prevent key type convertion
            echo '<pre>The key: '.(string) $key.'</pre>';

            if ((string)$key == $dataItemName)
                return true;

            if (is_array($value) && $this->checkKeyIsInArray($dataItemName, $value))
                return true;

        }
    return false;
}

BTW, why is this a non-static function? It doesn't seem to need any instance properties.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you Barmar - I will try your suggestions later this afternoon. Regarding your query about the method being static or not, you're right - it does not need instance properties. I am unsure, however, how making the method `static` will assist the application. The method exists inside my object as part of a process to mutate the data structure, which exists in each of of my value objects. At this stage I have not found another use for this method. If there is some other imperative for making it static then I should certainly look at that. Could you clarify why I should make it static? – Steve Cooke Dec 16 '13 at 00:42
  • 1
    It's just a coding purity reason. If a method isn't dependent on the instance, then making it static makes this clear. There might also be some efficiencies in the implementation, since it's one less parameter that has to be passed (non-static methods receive `$this` as an implicit parameter) and method lookup doesn't have to scan virtual tables. – Barmar Dec 16 '13 at 18:18
  • 1
    Thank you Barmar. +1 for the heads up regarding static methods - food for thought. – Steve Cooke Dec 17 '13 at 01:08