Is there a way of doing something like this:
$test_array = array(\"first_key\" => \"first_value\",
\"second_key\" => \"second_valu
I always like the javascript variant of array map. The most simple version of it would be:
/**
* @param array $array
* @param callable $callback
* @return array
*/
function arrayMap(array $array, callable $callback)
{
$newArray = [];
foreach( $array as $key => $value )
{
$newArray[] = call_user_func($callback, $value, $key, $array);
}
return $newArray;
}
So now you can just pass it a callback function how to construct the values.
$testArray = [
"first_key" => "first_value",
"second_key" => "second_value"
];
var_dump(
arrayMap($testArray, function($value, $key) {
return $key . ' loves ' . $value;
});
);