jehzlau
jehzlau

Reputation: 627

JSON dump echo specific data only

So I have a JSON dump that looks like this:

{"LTC":{"PHP":2568.85},"ETH":{"PHP":18688.15},"IOT":{"PHP":29.91},"XRP":{"PHP":16.62},"BTC":{"PHP":154192.66}}

But I want to echo, for example ETH's PHP value only in a specific div. Then BTC's PHP value on another unique div.

So I did this for BTC:

echo $json['PHP']->BTC;

And this for ETH:

echo $json['PHP']->ETH;

But it doesn't seem to work. Where did I go wrong?

It's now working. Thanks for all your answers. :D

Upvotes: 0

Views: 188

Answers (3)

Bhavin Thummar
Bhavin Thummar

Reputation: 1293

Please check this code

    <?php


    $json = '{"LTC":{"PHP":2568.85},"ETH":{"PHP":18688.15},"IOT":
    {"PHP":29.91},"XRP":{"PHP":16.62},"BTC":{"PHP":154192.66}}';

    $json = json_decode($json);


    echo $json->ETH->PHP;

    echo '<br/>';
    echo $json->BTC->PHP;

Upvotes: 1

mos
mos

Reputation: 83

I guess the reason is that you try to access $json as an array, when it is an object. Also, you got the order of PHP and ETH wrong (you have to call the outermost key first):

$json = json_decode('{"LTC":{"PHP":2568.85},"ETH":{"PHP":18688.15},"IOT":{"PHP":29.91},"XRP":{"PHP":16.62},"BTC":{"PHP":154192.66}}');

print_r($json);

echo $json->ETH->PHP

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

Not sure what you are trying to do, but to get your right output, you just need to use:

<?php
  $json = '{"LTC":{"PHP":2568.85},"ETH":{"PHP":18688.15},"IOT":{"PHP":29.91},"XRP":{"PHP":16.62},"BTC":{"PHP":154192.66}}';
  $json = json_decode($json);
  echo $json->ETH->PHP;
  echo $json->ETH->BTC;
?>

Upvotes: 3

Related Questions