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

4 Answers 4

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]);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Recursion should do the job better.
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.

1 Comment

the second console statement is an example of the value, this will return the next object down in some cases
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']
    }
}

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.