Placing a multidimensional array in Javascript with PHP using JSON

My php script sends back the encoded JSON string.

I just lost how to actually use an array, now it sits well in Javascript?

The ultimate goal is to loop through a multidimensional array in JavaScript to extract values ​​(prices) ...

I managed to get JavaScript to get the encoded string (tested by printing it on the screen), but I'm not sure how I can actually use the array, or how I would scroll it like in PHP ..

I basically need to make the JavaScript equivalent for this PHP code

 foreach ($array as $item => $value){

    foreach ($value as $item2 => $value2){

      //peform action on $value2;
    }
}

Thanks for any help.

Oz

+3
source share
2 answers

, arrayFromPhp, for:

for(var i = 0, l = arrayFromPhp.length; i < l; i++) {
  for(var j = 0, l2 = arrayFromPhp[i].length; j < l2; j++) {
    var value = arrayFromPhp[i][j];
    //Do stuff with value
  }
}
+2

jquery json:

$.each(obj, function(key, value) {
    if ($.type(value) == "object") {
       $.each(value, function(key, value) {
           // value would be $value2 here
       })
    }
});

, json- PHP, http://api.jquery.com/jQuery.parseJSON/, json

var obj = jQuery.parseJSON(stringFromPhp);

$.getJSON() (http://api.jquery.com/jQuery.getJSON/), json- .

edit: .

+2

Source: https://habr.com/ru/post/1790223/


All Articles