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月01日T00:00:00
[to_date] => 2017年09月30日T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017年09月01日T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017年09月01日T00:00:00
[to_date] => 2017年09月30日T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017年09月01日T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017年09月01日T00:00:00
[to_date] => 2017年09月30日T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017年09月01日T00:00:00
)
)
)
)
)
1 Answer 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.
}
<BR>
is not how you do it.