I want to come up with $desired_output from given $data array below using array_map() or any PHP array_* built-in function. Any idea how?
$data = array(
'posted_name' => 'John Doe',
'posted_age' => '30',
);
$desired_output = array(
'name' => 'John Doe',
'age' => '30',
);
I tried below but the result is an array of arrays:
$desired_output = array_map(
function($keys, $vals)
{
$new[substr($keys, strlen('posted_'))] = $vals;
return $new;
},
array_keys($data),
$data
);
print_r($desired_output );
/**
* Array (
[0] => Array ( [name] => John Doe )
[1] => Array ( [age] => 30 )
)
*/
array_keysto get the keys, map or walk those to change the key names, and splice together witharray_valuesagain usingarray_combine…? Sounds like a lot of effort to “avoid” a simple, self-written loop IMHO.