Home > Mobile >  Php array to javascript objet for simplemaps
Php array to javascript objet for simplemaps

Time:08-06

can you help me please for this?

I have this php array:

array:2 [▼
  0 => array:5 [▼
    "lat" => "48.3333"
    "lng" => "7.3"
    "name" => "name à VILLE (67)"
    "url" => "https://www.test.fr"
    "size" => "35"
  ]
  1 => array:5 [▼
    "lat" => "42.9167"
    "lng" => "3.03333"
    "name" => "partenaire à LEUCATE (11)"
    "url" => "http://www.test.fr"
    "size" => "35"
  ]

How can I turn it into a javascript array of this shape?

  "0": {
      lat: "48.866667",
      lng: "2.333333",
      name: "Paris",
      description: "description",
      url: "url",
      size: "size",
      image_url: "imgURL"
    },
    "1": {
      lat: "42.91011287515971",
      lng: "3.029037436797626",
      name: "Leucate",
      description: "description",
      url: "url",
      size: "size",
      image_url: "imgURL"
    }

I have try this:

$object = (object)array();
foreach ($depots as $keys => $value ) {
    $object->{$keys} = $value;
}

But I have this result:

{
 "0":
   {
    "lat":"48.3333",
    "lng":"7.3",
    "name":"name à VILLE (67)",
    "url":"https://www.test.fr",
    "size":"35"
   },
"1":
   {
   "lat":"42.9167",
   "lng":"3.03333",
   "name":"partenaire à LEUCATE (11)",
   "url":"http://www.test.fr",
   "size":"35"
  }
}

How to only remove the double quotes on the keys? The goal is to add this javascript object afterwards to generate a simplemaps map!

Thanks!

CodePudding user response:

Try <?=json_encode($array)?>

CodePudding user response:

From javascript side, you can just convert this object to array like this:

Object.keys(phpArr).map(key => phpArr[key]);

  • Related