0

hello I am working on a script that posts a set of json object data to a php file

the json data is stored in a variable myJSON as {"Peter":"35","Ben":"37","Joe":"43"}

and is passed below to Ajax post request

var data = {
  test: myJSON
};
var options = {
  url: "###############.php",
  dataType: "text",
  type: "POST",
  data: { test: JSON.stringify( data ) },
  success: function( data, status, xhr ) {
     alert(data);
  },
  error: function( xhr, status, error ) {
      alert(status);
      //...
  }
};
$.ajax( options );

Next thing in my php script I receive the posted data and I do a var_dump

 $someJSON = $_POST['test'];
  echo $array = json_decode($someJSON, true );
    var_dump($array); 

which results in the below screenshot

var_dump image

but when I try to print this same data with a for each loop e.g trying to get the details of "Peter"

 foreach($array as $item) { 
    echo $item['Peter']; 
}

I get this error below

error

how do I correct this, been stuck on this, please help

0 Answers0