-3

I was trying to get the price_usd but i tried use this

data["price_usd"]

and does not help so how do i suppose to do to get the data?

json table:

[
    {
        "id": "bitcoin-cash", 
        "name": "Bitcoin Cash", 
        "symbol": "BCH", 
        "rank": "3", 
        "price_usd": "2957.36", 
        "price_btc": "0.211189", 
        "24h_volume_usd": "1339490000.0", 
        "market_cap_usd": "49908111624.0", 
        "available_supply": "16875900.0", 
        "total_supply": "16875900.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-0.09", 
        "percent_change_24h": "2.4", 
        "percent_change_7d": "34.95", 
        "last_updated": "1514247253"
    }
]
Goma
  • 2,018
  • 1
  • 10
  • 19
zhiyan114
  • 125
  • 1
  • 8
  • 1
    you have to decode your Json data into an array : take a see here : https://stackoverflow.com/questions/7511821/how-to-convert-json-string-to-array – Yassine CHABLI Dec 26 '17 at 00:27
  • 1
    Your question shows [no attempt](//idownvotedbecau.se/noattempt/) of solving the problem. If you have made an attempt, you should edit our question to detail exactly what you did, researched for, and point to any links that were helpful but that did not answer your question. If you’ve tried to code a solution, that should be added in an edit. Your attempts should be turned into a [MCVE](//stackoverflow.com/help/mcve) so it is clear to read and understand. Also read the [Stack Overflow question checklist](//meta.stackoverflow.com/questions/260648) – Filnor Dec 26 '17 at 00:44
  • @MOHAMMEDYASSINEChabli that actually helped me a bit thanks – zhiyan114 Dec 26 '17 at 00:55

2 Answers2

0

You First need to convert the json text into a php array/object.

Use this code as an example:

<?php
$rawJSon="your long json here"
$data=json_decode($rawJson, true);
echo $data[0]["price_usd"];
  • gave me this error: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/testenv.php:10\nStack trace:\n#0 {main}\n thrown in /var/www/html/testenv.php – zhiyan114 Dec 26 '17 at 00:48
  • Sorry I forget to add a parameter. Try the new code –  Dec 26 '17 at 00:55
0

Assuming this is your JSON string:

$raw = <<<EOT
[
    {
        "id": "bitcoin-cash", 
        "name": "Bitcoin Cash", 
        "symbol": "BCH", 
        "rank": "3", 
        "price_usd": "2957.36", 
        "price_btc": "0.211189", 
        "24h_volume_usd": "1339490000.0", 
        "market_cap_usd": "49908111624.0", 
        "available_supply": "16875900.0", 
        "total_supply": "16875900.0", 
        "max_supply": "21000000.0", 
        "percent_change_1h": "-0.09", 
        "percent_change_24h": "2.4", 
        "percent_change_7d": "34.95", 
        "last_updated": "1514247253"
    }
]
EOT;

Since you have a JSON object you need to use the arrow too specify an object property (notice the object curly braces in your JSON):

$data = json_decode( $raw );
echo $data[0]-> price_usd;

Or you can decode as an array instead of an object:

$data = json_decode( $raw, TRUE );
echo $data[0]['price_usd'];
C Miller
  • 428
  • 2
  • 12