I have an array such as ['id' => 1, 'name' => 'Fred']
.
I want to call array_map
on this array and also use the key inside the function. However, when I make a return, my keys will become indexed.
Simple example :
$arr = array('id' => 1, 'name' => 'Fred');
$result = array_map(
function ($value, $key) {
return $value;
},
$arr,
array_keys($arr)
);
var_dump($result);
Basically, I want $result
to be identical to $arr
in this case, but without reindexing my array.
array_map()
is an inappropriate tool for this job. Show us what you actually want to do with this daya.array_map()
does not kill keys if you just pass in the array. 3v4l.org/HWH9C If you pass in multiple arrays, of course, it cannot know which keys to preserve. 3v4l.org/KJbaV This seems like an XY Problem.