Pass a PHP Closure to array_map to Look Up Values by Key
Map a list of PHP keys to their values with a PHP 7.4 arrow function or an anonymous function, and see exactly what each callback receives.
If you have a lookup array and a list of its keys, pass array_map a callback that accepts one key and returns $values[$key]. The keys come from the list passed to array_map; the lookup array is available where you create the callback. You can pass a PHP 7.4 arrow function directly, assign it to a variable first, or use a traditional anonymous function when you need a longer body. The examples below use only keys present in the lookup array.
Map existing keys with an arrow function
This complete example requires PHP 7.4 or later. It asks for the values associated with green, then red, rather than reading the lookup array in the order it was written:
<?php
$values = [
'red' => 'apple',
'green' => 'pear',
'blue' => 'berry',
];
$keys = ['green', 'red'];
$lookup = fn ($key) => $values[$key];
$result = array_map($lookup, $keys);
echo implode(', ', $result), "\n";
The displayed result is:
pear, apple
Read the call from right to left if the two arrays are easy to confuse. $keys is the input list: each of its entries is supplied to the callback as $key. $values is not another callback argument. It is the lookup array available when $lookup is defined, and the arrow function automatically captures it. For each supplied key, the expression $values[$key] provides the result. The PHP arrow-function manual documents that automatic capture; the arrow-functions RFC illustrates this same key-to-value use of array_map.
Assigning the callback to $lookup is useful when you want to name the operation before passing it. For a one-off call, you can put the same arrow function directly in the callback position:
$result = array_map(fn ($key) => $values[$key], $keys);
That line can replace the $lookup assignment and the following array_map call in the complete example. It uses the same $values and $keys, so it produces the same displayed values. In either spelling, the first argument to array_map is the callback, while $keys supplies its inputs. If you want to review how the lookup itself reads an array entry, see Create a PHP Array, Then Read and Update Its Items.
Write the callback with an explicit return
If the callback needs a longer body, or the code cannot use PHP 7.4 arrow syntax, use a traditional anonymous function. Here is the equivalent complete example, with the same lookup and key order:
<?php
$values = [
'red' => 'apple',
'green' => 'pear',
'blue' => 'berry',
];
$keys = ['green', 'red'];
$lookup = function ($key) use ($values) {
return $values[$key];
};
$result = array_map($lookup, $keys);
echo implode(', ', $result), "\n";
It displays:
pear, apple
The job has not changed. $key still receives an entry from $keys, and the callback still returns the corresponding entry from $values. The syntax change is that this form names the needed lookup array in use ($values) and writes return explicitly. The arrow form instead captures $values automatically and returns its expression. The PHP manual on anonymous functions describes use as the way to bring a value from the defining scope into an anonymous function.
Both callback expressions produce Closure objects, whether you assign one to $lookup or place it directly in the call. For this task, choose the arrow expression when that single array access says everything the callback needs to do. Choose the traditional form when you need room for more statements or cannot use arrow syntax. Neither choice changes which list supplies $key or which array supplies the looked-up value.
