PHP – Transform an Array into another format

You can transform an array into another form with the following snippet.

$datasource = ['lemons' => 1, 'cherries' => 2, 'mango' => 3];

$translation = array_map(function ($key, $value) {
    return ['name' => $key, 'quantity' => $value];
}, array_keys($datasource), $datasource);

var_dump($translation);

 

Gareth
Buy Me A Coffee
back arrowBack to Index