Answer the question
In order to leave comments, you need to log in
How to display latest prices by latest date from JSON array?
There is a json array of this kind, you need to display the name and price of the last date.
{"Name1":{"2016-06-22":{"price":677,"count":71},"2016-06-23":{"price":625,"count":67},"2016-06-24":{"price":628,"count":78}},"Name2":{"2016-06-22":{"price":2536,"count":46},"2016-06-23":{"price":2432,"count":40},"2016-06-24":{"price":2396,"count":142}}}
function handle() {
$url = "730.json";
$content = file_get_contents($url);
$priceFull = json_decode($content);
$priceClear = [];
foreach ($priceFull AS $itemName => $itemPricesByDate) {
$oPrice = array_values($itemPricesByDate);
$oItem = [ $itemName => $oPrice[-1]['price'] ];
$priceClear[] = $oItem;
@file_put_contents('Update.log', json_encode($oItem) . PHP_EOL, FILE_APPEND);
}
$priceClearJson = json_encode($priceClear);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question