1

I have a php multidimensional array and i convert it with into JSON with php json_encode function.

Now, after encoding i echo the json like echo 'var javascriptJson = '.$encoded_in_json_array;

How can i get the values of javascriptJson using javascript ? Or else how can i convert my multidim php array into javascript so i can iterate through it or get the values ?

javascriptJson looks like that :

var javascriptJson={
  "AMERICA CENTRALA SI DE SUD": {
    "Argentina": {
      "Recomandari pentru toti calatorii: Vaccinari de rutina": {
        "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;",
        "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;",
        "Varicela": "background-color: ffffff;",
        "Gripa sezoniera": "background-color: ffffff;"
      },
      "Recomandari de vaccinare pentru majoritatea calatorilor": {
        "Hepatita A": "background-color: a6a6a6;",
        "Tifos": "background-color: d9d9d9;"
      },
      "Recomandari  de vaccinare limitate (in functie de expunere)": {
        "Hepatita B": "background-color: 7f7f7f;",
        "Rabia": "background-color: 93d;"
      },
      "Recomandari speciale de preventie": {
        "Malaria": "background-color: 9bbb59;"
      }
    },
    "Belize": {
      "Recomandari pentru toti calatorii: Vaccinari de rutina": {
        "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;",
        "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;",
        "Varicela": "background-color: ffffff;",
        "Gripa sezoniera": "background-color: ffffff;"
      },
      "Recomandari de vaccinare pentru majoritatea calatorilor": {
        "Hepatita A": "background-color: a6a6a6;",
        "Tifos": "background-color: d9d9d9;"
      },
      "Recomandari  de vaccinare limitate (in functie de expunere)": {
        "Hepatita B": "background-color: 7f7f7f;",
        "Rabia": "background-color: 93d;"
      },
      "Recomandari speciale de preventie": {
        "Malaria": "background-color: 9bbb59;"
      }
    },
    "Bolivia": {
      "Recomandari pentru toti calatorii: Vaccinari de rutina": {
        "Difterie\/Tetanos\/Pertussis": "background-color: b6c86;",
        "Rujeola\/Rubeola\/Oreion": "background-color: b1ffff;",
        "Varicela": "background-color: ffffff;",
        "Gripa sezoniera": "background-color: ffffff;"
      },
      "Recomandari de vaccinare pentru majoritatea calatorilor": {
        "Hepatita A": "background-color: a6a6a6;",
        "Tifos": "background-color: d9d9d9;"
      },
      "Recomandari  de vaccinare limitate (in functie de expunere)": {
        "Hepatita B": "background-color: 7f7f7f;",
        "Rabia": "background-color: 93d;"
      },
      "Recomandari speciale de preventie": {
        "Malaria": "background-color: 9bbb59;"
      }
    }, .....etc...etc
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
adrian F
  • 119
  • 1
  • 8
  • Maybe this helps you with the problem : http://stackoverflow.com/questions/13994858/javascript-convert-php-json-into-a-javascript-array – Daniel Bejan Mar 06 '14 at 15:14
  • By using `javascriptJson["AMERICA CENTRALA SI DE SUD"]["Belize"]["Recomandari speciale de preventie"]["Malaria"]`? – h2ooooooo Mar 06 '14 at 15:17
  • @h2ooooooo he needs an automatic way to do this... – Daniel Bejan Mar 06 '14 at 15:22
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Mar 06 '14 at 16:21

4 Answers4

1

If you want to loop over an object you can do

foreach(prop in obj){ 
console.log(prop); // would get you the property name
console.log(obj[prop]); // would get you the value of that property
}

That could get you started in this instance.

danblundell
  • 1,443
  • 12
  • 7
1
for (var i in javascriptJson) {
  console.log(javascriptJson[i]);
  for (var j in javascriptJson[i]) {
     console.log(javascriptJson[i]);
     console.log(javascriptJson[i][j]);
  }
}
Brant Olsen
  • 5,628
  • 5
  • 36
  • 53
Johan Sundén
  • 447
  • 4
  • 9
1

From the looks of things you should have a javascript object available after the var javascriptJson = '.$encoded_in_json_array; line.

You can iterate over this object using something like:

for(region in javascriptJson){
    var region_object = javascriptJson[region];
    // Code to handle each region
    // If you want to look at each country in a region...
    for(country in region_object){
        var country_object = region_object[country];
        // Code to handle each country object
        // You can get values like:
        // country_object['Recomandari pentru toti calatorii: Vaccinari de rutina']
    }
}
turtlemonvh
  • 9,149
  • 6
  • 47
  • 53
1

Your question

How can i get the values of javascriptJson using javascript ? Or else how can i convert my multidim php array into javascript so i can iterate through it or get the values ?

The solution

var myObject = JSON.parse(/*json-string*/)

Now you can iterate through the values like this:

for (var property in myObject) {
  // Do whatever you want
}
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89