Receiving JSON Data - PHP

0

Good afternoon. I tried to search here, but I did not find what I needed. I'm working with JSON, and I have received the following return string:

{
    "success": 1,
    "return": {
        "100025362": {
            "pair": "BRL",
            "type": "sell",
            "amount": 21.615,
            "rate": 0.258,
            "timestamp_created": 1418654530,
            "status": 0
        },
        ...
    }
}

This number 100025362 , is an open order that I have.

But at the time of this JSON query, I do not have the order number.

I would need to run all orders, to get to status.

If I had the order number, I would have no problem, I would:

$json_ActiveOrder = json_decode($stringJSON);
$ActiveOrderStatus = $json_ActiveOrder->return->100025362->status;

But since I do not have access to the number at this time, how would I run all the orders? In the case 100025362 ??

I tried to receive only up to return , and loop, but I have the following errors with the code below:

$json_ActiveOrder = json_decode($retorno_ActiveOrder);
$ActiveOrder = $json_ActiveOrder->return;

foreach($ActiveOrder as $f) {
    echo $f[0];
}

The error exits:

( ! ) Notice: Undefined property: stdClass::$return in C:\wamp\www\bitcoin\index.php on line 132
Call Stack
#   Time    Memory  Function    Location
1   0.0000  149328  {main}( )   ..\index.php:0

( ! ) Warning: Invalid argument supplied for foreach() in C:\wamp\www\bitcoin\index.php on line 134
Call Stack
#   Time    Memory  Function    Location
1   0.0000  149328  {main}( )   ..\index.php:0
    
asked by anonymous 19.01.2017 / 18:30

2 answers

1

You can get the array key in foreach:

foreach($ActiveOrder as $key => $f) {
    echo $key;
}

The first error that appears is that there is no property return and the second because $ActiveOrder is not an array

    
19.01.2017 / 18:33
0

Do so, change the line:

$json_ActiveOrder = json_decode($retorno_ActiveOrder);

To:

$json_ActiveOrder = json_decode($retorno_ActiveOrder,true);

This will cause the $ json_ActiveOrder variable to be an Associative Array

If you want to take a look at the documentation: link

    
16.03.2018 / 18:02