Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of PHP use keyword in array_map() function?

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.

like image 351
myprogram Avatar asked Dec 04 '17 17:12

myprogram


1 Answers

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.

like image 194
Camilo Avatar answered Sep 22 '22 00:09

Camilo