0

I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now

$json_array = (array)(json_decode($response));

When I print my JSON Decoded array, I have the following data below: I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.

Array ( 
    [resp_code] => SUCCESS 
    [caller_ref] => 2017092002282130006180 
    [server_ref] => 2017092002282169760291 
    [data] => stdClass Object ( 
        [type] => monthly
        [group_id] => 19 
        [device_id] => 32
        [sn] => sn1234
        [usages] => Array ( 
            [0] => stdClass Object (
                [from_date] => 2017-09-01T00:00:00 
                [to_date] => 2017-09-30T23:59:59 
                [up] => 22370
                [down] => 119217
                [ts] => 2017-09-01T00:00:00 
            )
        ) 
        [details] => stdClass Object (
            [3] => Array ( 
                [0] => stdClass Object (
                    [from_date] => 2017-09-01T00:00:00 
                    [to_date] => 2017-09-30T23:59:59 
                    [up] => 5522 
                    [down] => 40301 
                    [ts] => 2017-09-01T00:00:00 
                ) 
            ) 
            [2] => Array ( 
                [0] => stdClass Object ( 
                    [from_date] => 2017-09-01T00:00:00 
                    [to_date] => 2017-09-30T23:59:59
                    [up] => 6905 
                    [down] => 32029 
                    [ts] => 2017-09-01T00:00:00
                ) 
            )
        )
     )
)
2
  • Can you provide me the actual JSON string? Commented Sep 20, 2017 at 3:01
  • @JustinF Take 5 mins to learn the editor, adding <BR> is not how you do it. Commented Sep 20, 2017 at 3:01

1 Answer 1

1

Whats wrong with objects?

$obj = json_decode($response);

echo $obj->data->details[0]->from_date;

Or to loop it:

foreach ($obj->data->details as $item) {
    echo $item->from_date;
    // same goes for: to_date, up etc.
}

Simple and sexy!

Update: It looks as if $item would be an array as well, so if you have problems try:

foreach ($obj->data->details as $item) {
    echo $item[0]->from_date;
    // same goes for: to_date, up etc.
}
Sign up to request clarification or add additional context in comments.

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.