$prices = ['apple' => 2, 'pear' => 3];
$doubled = array_map(fn($p) => $p * 2, $prices);
print_r($doubled);What is printed?
- aArray ( [0] => 4 [1] => 6 ) — array_map always renumbers keys starting from 0
- bArray ( [apple] => 2 [pear] => 3 ) — the closure is ignored because array_map cannot take arrow functions
- cArray ( [apple] => 4 [pear] => 6 ) — original keys are preserved, each value doubled✓
- dA fatal error, because array_map cannot be combined with a single input array
Explanation:array_map() preserves the original array's keys whenever exactly one input array is passed — only when multiple arrays are passed does it reindex the result numerically. Here each value is doubled (22=4, 32=6) while the string keys 'apple' and 'pear' stay attached to their new values.