Processing JSON array in PHP -


i want able use json object received post inside php file. json looks this:

array(1) {   ["json"]=>   string(260) "[{"proddescr":"text1","prodpu":"1","prodcant":"1","prodnume":"text1x"},  {"proddescr":"text2","prodpu":"2","prodcant":"2","prodnume":"text2x"}, {"proddescr":"text3","prodpu":"price:150.00","prodcant":"quantity:4","prodnume":"text3x"}]" } 

i access inside php file:

<?php   header('content-type: application.json');  $x = json_decode($_post['json']);  foreach($x $i => $value){    print_r($x[$i]);  } ?> 

now... coming desktop programming... not know json processing, need able access elements of json array (3 seen above) , contents. seem able access main elements using foreach, cannot seem succeed in accessing inside elements of each "record"

but result looks this:

stdclass object (     [proddescr] => text1     [prodpu] => 1     [prodcant] => 1     [prodnume] => text1x ) stdclass object (     [proddescr] => text2     [prodpu] => 2     [prodcant] => 2     [prodnume] => text2x ) , on 

the purpose able compose insert statement based on values json array.

so need able (inside foreach loop) "proddescr" value, "prodpu" value, "prodcant" value , "prodnume" value each of 3 (in case) array items.

i tried

   print_r($x[$i][0]); 

also

   print_r($x[$i]["proddesc"]); 

in order able access inside values of array not work (i keep getting "500 internal server error" when add above 2 print_r.

how can access these sub-values of array?

use true second parameter in json_decode convert array

$x = json_decode($_post['json'],true); foreach($x $i => $value){     echo $x[$i]['proddescr'];     echo $x[$i]['prodpu'];     echo $x[$i]['prodcant'];     echo $x[$i]['prodnume']; } 

codepad demo


Comments