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 int.
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 it turns my string keys into ints.
For your requirement of "$result to be identical to $arr", try:
$result = array_combine(
array_keys($arr),
array_map(function($v){ return $v; }, $arr)
);
Gives:
[
"id" => 1
"name" => "Fred"
]