0

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?

3 Answers 3

2

Use true as second parameter in json_decode to convert it to array

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

Codepad demo

Sign up to request clarification or add additional context in comments.

1 Comment

You answered first so I accepted your answer. However @DevZer0 had a very nice point in his answer and improved a lot my understanding of PHP. I only marked his answer with "useful" because you answered first, although his answer is better.
2

There is couple of issues here, first your using a foreach block but accessing the array as if you were using a for loop. You don't need to set the header here because your not outputting any json.

$x = json_decode($_POST['json']);
 foreach($x as $i => $value){
     echo $value->proddescr; //you can access the other objects the same way.
 }

You can also access it using the other methods. but based on your iteration setup this method provides the cleanest access.

1 Comment

Your answer also works. How can I check two good answers here?
0

I would say $x[$i]->proddescr etc

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.