Fusseldieb Fusseldieb - 1 year ago 100
PHP Question

Trying to read StdClass Object in PHP

I decoded a JSON array which contained keys and values in PHP. The JSON looks like this (shortened for easier understanding):

[{"code":"123"},{"identification":"Some item"},{"price":"$20"}]

After I
'd it, it looked like this:

Array ( [0] => stdClass Object ( [code] => 123 ) [1] => stdClass Object ( [identification] => Some item ) [2] => stdClass Object ( [price] => $20 ) )

How can I read both the key and the value?

I already tried searching on SO all over and already tried something like this:

foreach ($jarray as $key) {
echo 0->$key;

which throws an 500 (ISE) error.

Also tried this:

foreach ($jarray as $key => $value) {
echo $key;
echo $value;

which also throws an 500 error.

I don't know how to accomplish this...

EDIT: Basically, I just want to iterate trough the whole thing and get key and value every time, like this:

code - 123
identification - Some item
price - $20

Answer Source
$json = '[{"code":"123"},{"identification":"Some item"},{"price":"$20"}]';
$jarray = json_decode($json, true);

foreach ($jarray as $value) {
    foreach ($value as $key => $val) {
        echo $key;
        echo $val;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download