8

I've the following code of lines in my application. Can anybody please tell me what is the purpose of use keyword in the following array_map() function?

array_map( function($record) use ($edit_form, $otherfields, $otherfields_keys)
{
    User::parseData($record, $edit_form['metadata']);

    if (isset($otherfields[$record['user_id']])) {
        return $record + $otherfields[$record['user_id']];
    }

    return $record + $otherfields_keys;

}, $records);

Thanks in advance.

Camilo
  • 6,504
  • 4
  • 39
  • 60
techsu
  • 753
  • 2
  • 7
  • 11
  • 3
    Possible duplicate of [In PHP 5.3.0, what is the function "use" identifier?](https://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier) – Zlatan Omerović Dec 04 '17 at 17:26

1 Answers1

9

The callback passed to array_map() doesn't have access to outside variables so they must be passed using use.

You can read more about anonymous functions in the PHP documentation.

Camilo
  • 6,504
  • 4
  • 39
  • 60