Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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

share|improve this answer
    
Thank you. Works great. – user1137313 Jul 11 '13 at 14:06
    
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. – user1137313 Jul 11 '13 at 14:13

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.

share|improve this answer
    
Your answer also works. How can I check two good answers here? – user1137313 Jul 11 '13 at 14:10
    
you can't but you can vote for mine – DevZer0 Jul 11 '13 at 14:13

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.