11

The following snippet returns a bunch of input fields but I'm unable to set their values because $data is undefined (it being inside a closure).

$row = array_map(function($n) {
    $name = sprintf('point[%0d]', $n+1);
    $value = $data['measurements'][$n];
    return form_input($name, $value, "class='input-mini'");
}, range($i*6, $i*6+5));

I know global variables are not cool. What's the best way to get around this?

1
  • 4
    Use function($n) use ($data) { Commented Nov 14, 2012 at 16:04

1 Answer 1

26

Inheriting variables from the parent scope

$row = array_map(function($n) use ($data) {
    $name = sprintf('point[%0d]', $n+1);
    $value = $data['measurements'][$n];
    return form_input($name, $value, "class='input-mini'");
}, range($i*6, $i*6+5));
Sign up to request clarification or add additional context in comments.

Comments

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.