I want to be able to use a JSON object received from POST inside a php file. The JSON looks like 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 it like this inside the php file:
<?php
header('Content-type: application.json');
$x = json_decode($_POST['json']);
foreach($x as $i => $value){
print_r($x[$i]);
}
?>
Now... coming from desktop programming... I do not know much about json processing, but I need to be able to access all elements of the JSON array (3 are seen above) and all their contents. I seem to be able to access the main elements using the foreach, but I cannot seem to succeed in accessing the inside elements of each "record"
But the result looks just like this:
stdClass Object
(
[proddescr] => text1
[prodpu] => 1
[prodcant] => 1
[prodnume] => text1x
)
stdClass Object
(
[proddescr] => text2
[prodpu] => 2
[prodcant] => 2
[prodnume] => text2x
)
and so on
The purpose is to be able to compose an INSERT statement based on the values from the json array.
So I need to be able (inside the foreach loop) to get the "proddescr" value, "prodpu" value, "prodcant" value and "prodnume" value from each of those 3 (in this case) array items.
I tried
print_r($x[$i][0]);
also
print_r($x[$i]["proddesc"]);
in order to be able to access the inside values of the array but does not work (I keep getting "500 Internal server error" when I add the above two print_r.
How can I access these sub-values of my array?