1

I have a method to check whether a key is in a nested associative array:

private function checkKeyIsInArray($dataItemName, $array) {      
    foreach ($array as $key=>$value) {
        if ($key == $dataItemName) return true;
        if (is_array($value)) {
            checkKeyIsInArray($dataItemName, $value);
        }
    }
    return false;
}

It always returns true, regardless of the keys I include or do not include. Here is my test array:

Array
(
[0] => Array ( [reset_time] => 2013-12-11 22:24:25 )
[1] => Array ( [email] => [email protected] )
)

Please, what am I doing wrong? If I search for "reset_time" the method returns true (as I expect); when I search for "reset_expired" the method also returns true (which is incorrect).

2 Answers 2

1

Your method is almost works. But there is few issues.

  1. Comparsion numeric values and strings. In first round method has 0 as key and 'email' as value. 0 == 'email' always returns true.

  2. You should use $this when calling object member function.

  3. You should return value of recursive function.

Your rewrited method.

class check
    {

    private function checkKeyIsInArray($dataItemName, $array)
        {
        foreach ($array as $key => $value)
            {
            // convert $key to string to prevent key type convertion
            if ((string) $key == $dataItemName)
                return true;
            if (is_array($value))
            // $this added
            // return added
                return $this->checkKeyIsInArray($dataItemName, $value);
            }
        return false;
        }

    public function myCheck($dataItemName, $array)
        {
        return $this->checkKeyIsInArray($dataItemName, $array);
        }

    }

$check = new check();
$array = array(array('reset_time' => 123, 'email' => 123));
var_dump($check->myCheck('reset_time', $array)); // true
var_dump($check->myCheck('reset_expired', $array)); // false
var_dump($check->myCheck('0', $array)); // true
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Sectus - this looks correct. I will add the suggested alterations later this afternoon and test, but just reading your code makes sense to me.
1

I have updated your own code, there was some minor issue.please check.

function checkKeyIsInArray($dataItemName, $array) {      
   foreach ($array as $key=>$value) {
    ## here $key is int and $dataItemName is string so its alway comes true in ur case
    if ("$key" == $dataItemName) {
         return true;
    }
    else if (is_array($value)) {
        $returnvalue=checkKeyIsInArray($dataItemName, $value);
        ## once a matching key found  stop further recursive call
        if($returnvalue==true){
          return true;
        }
    }
  }
   return false;
}

1 Comment

Thank you for your input Praveen. Your solution is similar to the one suggested by Sectus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.