How can Anonymous Function convert to Normal function?

Writing some code in anonymous function, so I am testing, how the same code works with the normal function. Kindly help me to integrate same callable function to normal function.

**Callable function:**

print_r(array_map(function($elements){return $elements['last_name'];}, $records));

**Normal function:**

function elements($elements)
{
    return $elements['last_name'];
// Where to write variable $records?
}

Equivalent for

print_r(array_map(function($elements){return $elements['last_name'];}, $records));

using normal function would be

function element($element) {
    return $element['last_name'];
}

print_r(array_map('element', $records));

So basically, to write short code for temporary, we can use callback/Anonymous function, and for repeat functions, we can use a normal function?

not necessarily. you can save an anonymous function in a variable.

$fn = function ($elements) {
    return $elements['last_name'];
};

$result = array_map($fn, $records);
2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.