I have a json column in MySQL table that contains data of this sample:
[
[
-79.4936969567481,
37.3504643767627
],
[
-79.4936969567481,
37.3504643767627
]
]
I have a function that I would like to append ‘lat’ => and ‘lng’ => keys before the first value and second value of the array members respectively. So far I have:
$polygons = Polygon::get();
foreach ($polygons as $polygon) {
$coordinates = $polygon->coordinates;
// Transform the coordinates
$newCoordinates = array_map(function ($coordinate) {
return [
'lat' => $coordinate[0],
'lng' => $coordinate[1],
];
}, $coordinates);
// Update the model's coordinates column
$polygon->coordinates = $newCoordinates;
$polygon->save();
}
However, its resulting in
[
{
"lat": -79.4936969567481,
"lng": 37.3504643767627
},
{
"lat": -79.4936789563065,
"lng": 37.3504513771715
}
]
What could I be doing wrong? The { should be replaced by [, } by ] and : by =>