0

I am using CakePHP 2.4.4. In the Controller I am setting an array :

$bla = array();
$bla[] = 'phone';
$bla[] = 'id';
$this->set(compact('bla'));

In the view when I try to debug this $bla array it debugs well. But when I try to check if one string is in this array it gives me the Undefined variable: bla error. The whole view's code:

     array_walk_recursive($data, function(&$val, $key) {
        if (is_numeric($val) AND in_array($key, $bla)) { //this line gives me error: Undefined variable $bla, but it is actually defined
            if (ctype_digit($val)) {
                $val= (int) $val;
            } else {
                $val = (float) $val;
            }
        }
    });
1
  • Anonymous function has no access to $bla Commented Mar 30, 2017 at 11:29

1 Answer 1

2

Anonymous function you've created in array_walk_recursive has no access to $bla and any other outer variables. You should explicitly pass this variable to this function with use:

array_walk_recursive($data, function(&$val, $key) use ($bla) {
    if (is_numeric($val) AND in_array($key, $bla)) {
        if (ctype_digit($val)) {
            $val= (int) $val;
        } else {
            $val = (float) $val;
        }
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for very fast answer. You are very right!

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.