tklein
tklein

Reputation: 77

Pulling nested variable from json API

I'm trying to pull the highlighted variable from this API:

{  
   "timestamp":{  
      "total":1486424886,
      "exchanges":{  
         "NEG":1486423855,
         "MBT":1486424738,
         "LOC":1486422237,
         "FOX":1486424483,
         "FLW":1486411044,
         "B2U":1486424811,
         "ARN":1486405596
      }
   },
   "ticker_24h":{  
      "total":{  
         "last":**3011.8756088755**, // <---
         "high":4073.32,
         "low":2631.58,
         ...

http://api.bitvalor.com/v1/ticker.json

This is my code so far:

<html>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
<table width="auto">
<tr><td>BTC/BRL (Bitvalor)</tr></td>
<tr><td>

<?php
$url1 = "http://api.bitvalor.com/v1/order_book_stats.json";
$fgc1 = file_get_contents($url1);
$json1 = json_decode($fgc1, true);
$price1 = $json1["ticker_24h.total.last"];
echo $price1;
?>

</tr></td>
</table>
</html>

What am I missing?

Upvotes: 0

Views: 35

Answers (1)

Scopey
Scopey

Reputation: 6319

You access decoded JSON like an associative array:

$price1 = $json1['ticker_24h']['total']['last'];

Make sure you use an isset in case the format of data changes or the response is not what you expect.

Upvotes: 2

Related Questions