array_map_assoc() With Keys

<?php

$hash = ['a' => 1, 'b' => 2];


print_r(array_map(function($v, $k) { return "$k: $v";},
                  $hash,
                  array_keys($hash)
                ));

?>

array_map() only provides the value of the array, not the key. To access the key, one must use the extra argument, and array_keys().

Beware that the order of the arguments is now value first, key second, not the usual $key => $value.

See Also

PHP Features

Last updated: 14 July 2026