I have an array of rows where one (visual) column of data has two similar but different keys. I would like to replace one of the keys so that the column has the same key in all rows.
My input array:
[
['Ttitle' => 'lilly', 'Price' => 1.75, 'Number' => 3],
['Title' => 'rose', 'Price' => 1.25, 'Number' => 15],
['Title' => 'daisy', 'Price' => 0.75, 'Number' => 25],
['Title' => 'nettle', 'Price' => 2.75, 'Number' => 33],
['Title' => 'orchid', 'Price' => 1.15, 'Number' => 7],
];
My desired result: (notice the key change for lilly
)
[
['Title' => 'lilly', 'Price' => 1.75, 'Number' => 3],
['Title' => 'rose', 'Price' => 1.25, 'Number' => 15],
['Title' => 'daisy', 'Price' => 0.75, 'Number' => 25],
['Title' => 'nettle', 'Price' => 2.75, 'Number' => 33],
['Title' => 'orchid', 'Price' => 1.15, 'Number' => 7],
];
When I attempted to use array_map()
, I manage to change all the keys to Title
, but I also delete all Title
values except for the Ttitle
key I just changed to Title
.
Code is as follows:
if (!empty($notmatchingarray))
{
$completearray = array_merge($notmatchingarray, $datastuff2);
$completearray = array_map(
function($complete) {
return array(
'Title' => $complete['Ttitle'],
'Price' => $complete['Price'],
'Number' => $complete['Number'],
);
},
$completearray
);
print_r($completearray);
}
Am I doing the wrong thing in using this array_map()
function? Should I be doing something else?