简体   繁体   中英

unable to get last value of JSON array in PHP

here is the JSON file in question. I need to get the last y value from the values array. This updates once a day so the code below is what I've tried, but won't update to the latest value:

<?php
$url = 'https://api.blockchain.info/charts/avg-block-size?format=json';
$data = file_get_contents($url);
$stats = json_decode($data, true);

$blocksize = $stats['values']['363']['y'];

echo $blocksize;
?>

thoughts?

Try using:

$lastValue = end($stats['values']);
$blocksize = $lastValue['y'];

您可以计算数组的大小和-1

$blocksize = $stats['values'][count($stats['values']) - 1]['y'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM