0

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?

2 Answers 2

4

Try using:

$lastValue = end($stats['values']);
$blocksize = $lastValue['y'];
Sign up to request clarification or add additional context in comments.

1 Comment

This could be improved by checking whether the $stats['values'] exists or not first.
1

You can count the array size and -1 from it

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

1 Comment

I was using the "count" completely wrong just now then refreshed the page. thanks!

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.