Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
    
Maybe this helps you with the problem : stackoverflow.com/questions/13994858/… –  csanonymus Mar 6 '14 at 15:14
    
By using javascriptJson["AMERICA CENTRALA SI DE SUD"]["Belize"]["Recomandari speciale de preventie"]["Malaria"]? –  h2ooooooo Mar 6 '14 at 15:17
    
@h2ooooooo he needs an automatic way to do this... –  csanonymus Mar 6 '14 at 15:22
    
possible duplicate of Access / process (nested) objects, arrays or JSON –  Felix Kling Mar 6 '14 at 16:21

4 Answers 4

up vote 1 down vote accepted
for (var i in javascriptJson) {
  console.log(javascriptJson[i]);
  for (var j in javascriptJson[i]) {
     console.log(javascriptJson[i]);
     console.log(javascriptJson[i][j]);
  }
}
share|improve this answer
    
Recursion should do the job better. –  WoIIe Mar 7 '14 at 7:54

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.

share|improve this answer
    
the second console statement is an example of the value, this will return the next object down in some cases –  danblundell Mar 6 '14 at 15:24

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']
    }
}
share|improve this answer

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
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.