I need some help please.
I have this array in php:
Array (
[0] => Array (
[name] => Ville1
[description] => adresse1
[lng] => -10.35
[lat] => 29.1833
)
[1] => Array (
[name] => Ville2
[description] => description2
[lng] => 12.61667
[lat] => 38.3833
)
)
How can I transform it in this format and add in a objet in javascript ?
locations: {
"0": {
lat: "48.3833",
lng: "12.61667",
name: "Ville1",
description: "adresse1"
},
"1": {
lat: "29.1833",
lng: "-10.35",
name: "Ville2",
description: "adresse2"
}
},
Thanks and sorry for the english..
Advertisement
Answer
Please update your PHP Code accordingly. It should be working for you.
$array = [
[
"name" => "Ville1",
"description" => "adresse1",
"lng" => -10.35,
"lat" => 29.1833
],
[
"name" => "Ville2",
"description" => "description2",
"lng" => 12.61667,
"lat" => 38.3833
]
];
foreach ($array as $keys => $value ) {
$object->{$keys} = $value;
}
$jsonStructure = json_encode($object);
echo $jsonStructure;
The output should be like this :
{
"0": {
"name": "Ville1",
"description": "adresse1",
"lng": -10.35,
"lat": 29.1833
},
"1": {
"name": "Ville2",
"description": "description2",
"lng": 12.61667,
"lat": 38.3833
}
}
Now you can use this $jsonStructure to your javascript.