Is there a way of doing something like this:
$test_array = array(
"first_key" => "first_value",
"second_key" => "second_value"
);
var_dump(
array_map(
function($a, $b) {
return "$a loves $b";
},
array_keys($test_array),
array_values($test_array)
)
);
But instead of calling array_keys and array_values, directly passing the $test_array variable?
The desired output is:
array(2) {
[0]=>
string(27) "first_key loves first_value"
[1]=>
string(29) "second_key loves second_value"
}
Not with array_map, as it doesn’t handle keys.
array_walk does:
It does change the array given as parameter however, so it’s not exactly functional programming (as you have the question tagged like that). Also, as pointed out in the comment, this will only change the values of the array, so the keys won’t be what you specified in the question.
You could write a function that fixes the points above yourself if you wanted to, like this: