Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

74
Visualizações
Array to property Object in Javascript

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..

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The solution is simple. The only think you have to do, is to pass the array to the php function json_encode.

This will convert automatically your array to JSON.

The same way, you could also convert PHP Objects to JSON.

Finally, there's also another function of PHP with the name json_decode that does the opposite. If you have a JSON string, you can convert it to PHP Array or Object.

The only difference, is that when you convert JSON objects, in PHP you get stdClass instances.

<?php

$array = [
    [
        "name" => "Ville1",
        "description" => "adresse1",
        "lng" => -10.35,
        "lat" => 29.1833
    ],
    [
        "name" => "Ville2",
        "description" => "description2",
        "lng" => 12.61667,
        "lat" => 38.3833
    ]
];

$jsonStructure = json_encode($array);

?>
<script>
var locations = <?php echo $jsonStructure; ?>;

// The following line should print in your browser console 
// the word: Ville1
console.log(locations[0].name);
</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda