M
M
Maxla932016-06-25 14:06:56
PHP
Maxla93, 2016-06-25 14:06:56

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}}}

I tried to withdraw, but I get null instead of the price.
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);
    }

You need to get output like:
[{"market_hash_name":"Name1","price":"6.28","updated":"2016-06-24"},{"market_hash_name":"Name2","price":" 27.96","updated":"2016-06-24"}]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2016-06-25
@xmoonlight

https://www.if-not-true-then-false.com/2009/php-ti...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question