-1

I have this site's output which gives me the value of 750+ altcoins.

I get the output, and decode it as $data.

Let's simplify it to:

[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "610.561", 
        "price_btc": "1.0", 
        "24h_volume_usd": "44104400.0", 
        "market_cap_usd": "9688885050.0", 
        "available_supply": "15868824.0", 
        "total_supply": "15868824.0", 
        "percent_change_1h": "0.12", 
        "percent_change_24h": "0.25", 
        "percent_change_7d": "-1.61", 
        "last_updated": "1473929966"
    }, 
    {
        "id": "ethereum", 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "rank": "2", 
        "price_usd": "12.0793", 
        "price_btc": "0.0197864", 
        "24h_volume_usd": "8171040.0", 
        "market_cap_usd": "1014555388.0", 
        "available_supply": "83991240.0", 
        "total_supply": "83991240.0", 
        "percent_change_1h": "0.21", 
        "percent_change_24h": "1.25", 
        "percent_change_7d": "4.92", 
        "last_updated": "1473929962"
    }, 
    {
        "id": "ripple", 
        "name": "Ripple", 
        "symbol": "XRP", 
        "rank": "3", 
        "price_usd": "0.00597615", 
        "price_btc": "0.00000979", 
        "24h_volume_usd": "1017240.0", 
        "market_cap_usd": "211062429.0", 
        "available_supply": "35317458440.0", 
        "total_supply": "99997205581.0", 
        "percent_change_1h": "-0.16", 
        "percent_change_24h": "1.53", 
        "percent_change_7d": "0.88", 
        "last_updated": "1473929942"
    }, 
    {
        "id": "litecoin", 
        "name": "Litecoin", 
        "symbol": "LTC", 
        "rank": "4", 
        "price_usd": "3.83018", 
        "price_btc": "0.00627398", 
        "24h_volume_usd": "1101600.0", 
        "market_cap_usd": "182184060.0", 
        "available_supply": "47565404.0", 
        "total_supply": "47565404.0", 
        "percent_change_1h": "-0.11", 
        "percent_change_24h": "0.14", 
        "percent_change_7d": "-3.61", 
        "last_updated": "1473929943"
    }, 
    {
        "id": "monero", 
        "name": "Monero", 
        "symbol": "XMR", 
        "rank": "5", 
        "price_usd": "10.4646", 
        "price_btc": "0.0171415", 
        "24h_volume_usd": "4920040.0", 
        "market_cap_usd": "134987751.0", 
        "available_supply": "12899466.0", 
        "total_supply": "12899466.0", 
        "percent_change_1h": "-0.35", 
        "percent_change_24h": "-2.47", 
        "percent_change_7d": "-14.04", 
        "last_updated": "1473929949"
    }, 
]

How do I get the price_usd of let's say, ripple? I know that I can do $data[3]['price_usd'], however the info is based on the rank. The rank changes. How do I get it to retrieve the value of ripple without using [1]? I've tried $data['bitcoin']['price_usd'], but to no avail.

I've searched stackexchange for answers on this, but people have only told me what I know so far. Not how to find the value of a specific entry.

Regards

Community
  • 1
  • 1
  • Quick answer. With a loop. – Jonnix Sep 15 '16 at 09:15
  • Possible duplicate of [How can I access an array/object?](http://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – Sherif Sep 15 '16 at 09:19
  • Don't see how this is a duplicate. That question is asking information which I already have stated is not what I want, "I've tried $data['bitcoin']['price_usd'], but to no avail." – Whyte the Weeabear Sep 15 '16 at 09:21
  • 2
    @WhytetheWeeabear First, your question boils down to a basic understanding of how to access array/object values in PHP. Second, `$data['bitcoin']['price_usd']` **clearly** wouldn't yield any expectation of a value in this data, since there is no `bitcoin` key anywhere in this JSON. Read the manual to learn how to use [`json_decode()`](http://php.net/json-decode), understand the differences between [Arrays](http://php.net/language.types.array) and [Objects](http://php.net/language.types.object) in PHP and try spending a little more time debugging your code. – Sherif Sep 15 '16 at 09:27
  • This is why I am asking for help. The JSON in your thread that you suggested is in a different format. – Whyte the Weeabear Sep 15 '16 at 09:42

8 Answers8

2
$price = null;
foreach ($data as $item) {
    if ($item["id"] == "bitcoin") {
        $price = $item["price_usd"];
        break;
    }
}

var_dump($price);
  • @Whyte the Weeabear, as you mentioned that you want only required coins from the json. So above code will not optimal, which it needs to visit the json every time for new coin mentioned in array. – Rohan Khude Sep 15 '16 at 10:30
1

suppose say you have list of needed coins mentioned in array which is any sequence of the json sequence. Then instead of checking the whole json data for each time. I have checked that if any id from array matched with the json id then select the price_usd. This will have the optimal results by just traversing the json once.

<?php
$data = file_get_contents('https://api.coinmarketcap.com/v1/ticker/');
$data = json_decode($data, true);
$need = array(
    'nexus',
    'bitcoin',
    'zombiecoin',
    'ambercoin'
);
foreach ($data as $key => $value) {
    if (in_array($data[$key]['id'], $need)) {
        echo $data[$key]['id'] . " = " . $data[$key]['price_usd'];
        echo "<br>";
    }
}
?>

Results -

bitcoin = 610.398
nexus = 0.0356638
ambercoin = 0.0104771
zombiecoin = 0.000121343
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
0

You can use a function like this:

function getPrice($data, $id) {
   $price = 0;

   foreach($data as $item) {
      if($item->id == $id) {
          $price = $item->price_usd;
      }
   }

   return $price;
}

And then you can use it for ripple case like this.

$dribblePrice = getPrice($data, 'ripple');

Hope this helps.

Mubashar Abbas
  • 5,536
  • 4
  • 38
  • 49
0

Use my customized function to generate a associative array,

function getAssociativeArray($result,$keyfield="") {
            $dataarray = array();
            $i=0;
            while($row = mysql_fetch_assoc($result)) {
                if($keyfield!="") $dataarray[$row[$keyfield]] = $row;
                else $dataarray[$i] = $row;
                $i++;
            }
            return $dataarray;
        }

Pass the $data array and the Keyfield as id

$data = getAssociativeArray($data,'id');

then you can access the result as $data['bitcoin']['price_usd'] without worrying about the rank of the element.

Sasikumar
  • 863
  • 5
  • 9
0

Here is a way to do it :

$data = json_decode('[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "610.561", 
        "price_btc": "1.0", 
        "24h_volume_usd": "44104400.0", 
        "market_cap_usd": "9688885050.0", 
        "available_supply": "15868824.0", 
        "total_supply": "15868824.0", 
        "percent_change_1h": "0.12", 
        "percent_change_24h": "0.25", 
        "percent_change_7d": "-1.61", 
        "last_updated": "1473929966"
    }, 
    {
        "id": "ethereum", 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "rank": "2", 
        "price_usd": "12.0793", 
        "price_btc": "0.0197864", 
        "24h_volume_usd": "8171040.0", 
        "market_cap_usd": "1014555388.0", 
        "available_supply": "83991240.0", 
        "total_supply": "83991240.0", 
        "percent_change_1h": "0.21", 
        "percent_change_24h": "1.25", 
        "percent_change_7d": "4.92", 
        "last_updated": "1473929962"
    }, 
    {
        "id": "ripple", 
        "name": "Ripple", 
        "symbol": "XRP", 
        "rank": "3", 
        "price_usd": "0.00597615", 
        "price_btc": "0.00000979", 
        "24h_volume_usd": "1017240.0", 
        "market_cap_usd": "211062429.0", 
        "available_supply": "35317458440.0", 
        "total_supply": "99997205581.0", 
        "percent_change_1h": "-0.16", 
        "percent_change_24h": "1.53", 
        "percent_change_7d": "0.88", 
        "last_updated": "1473929942"
    }, 
    {
        "id": "litecoin", 
        "name": "Litecoin", 
        "symbol": "LTC", 
        "rank": "4", 
        "price_usd": "3.83018", 
        "price_btc": "0.00627398", 
        "24h_volume_usd": "1101600.0", 
        "market_cap_usd": "182184060.0", 
        "available_supply": "47565404.0", 
        "total_supply": "47565404.0", 
        "percent_change_1h": "-0.11", 
        "percent_change_24h": "0.14", 
        "percent_change_7d": "-3.61", 
        "last_updated": "1473929943"
    }, 
    {
        "id": "monero", 
        "name": "Monero", 
        "symbol": "XMR", 
        "rank": "5", 
        "price_usd": "10.4646", 
        "price_btc": "0.0171415", 
        "24h_volume_usd": "4920040.0", 
        "market_cap_usd": "134987751.0", 
        "available_supply": "12899466.0", 
        "total_supply": "12899466.0", 
        "percent_change_1h": "-0.35", 
        "percent_change_24h": "-2.47", 
        "percent_change_7d": "-14.04", 
        "last_updated": "1473929949"
    }
]');

foreach ($data as $item) {
    if($item->id === 'ripple'){
      echo $item->price_usd;
      break;
    }
}

Note that you need to remove the trailing comma from the last item for json_decode to work.

Hope this helps

vincenth
  • 1,732
  • 5
  • 19
  • 27
0
 $json='[
                {
                    "id": "bitcoin", 
                    "name": "Bitcoin", 
                    "symbol": "BTC", 
                    "rank": "1", 
                    "price_usd": "610.561", 
                    "price_btc": "1.0", 
                    "24h_volume_usd": "44104400.0", 
                    "market_cap_usd": "9688885050.0", 
                    "available_supply": "15868824.0", 
                    "total_supply": "15868824.0", 
                    "percent_change_1h": "0.12", 
                    "percent_change_24h": "0.25", 
                    "percent_change_7d": "-1.61", 
                    "last_updated": "1473929966"
                }, 
                {
                    "id": "ethereum", 
                    "name": "Ethereum", 
                    "symbol": "ETH", 
                    "rank": "2", 
                    "price_usd": "12.0793", 
                    "price_btc": "0.0197864", 
                    "24h_volume_usd": "8171040.0", 
                    "market_cap_usd": "1014555388.0", 
                    "available_supply": "83991240.0", 
                    "total_supply": "83991240.0", 
                    "percent_change_1h": "0.21", 
                    "percent_change_24h": "1.25", 
                    "percent_change_7d": "4.92", 
                    "last_updated": "1473929962"
                }, 
                {
                    "id": "ripple", 
                    "name": "Ripple", 
                    "symbol": "XRP", 
                    "rank": "3", 
                    "price_usd": "0.00597615", 
                    "price_btc": "0.00000979", 
                    "24h_volume_usd": "1017240.0", 
                    "market_cap_usd": "211062429.0", 
                    "available_supply": "35317458440.0", 
                    "total_supply": "99997205581.0", 
                    "percent_change_1h": "-0.16", 
                    "percent_change_24h": "1.53", 
                    "percent_change_7d": "0.88", 
                    "last_updated": "1473929942"
                }, 
                {
                    "id": "litecoin", 
                    "name": "Litecoin", 
                    "symbol": "LTC", 
                    "rank": "4", 
                    "price_usd": "3.83018", 
                    "price_btc": "0.00627398", 
                    "24h_volume_usd": "1101600.0", 
                    "market_cap_usd": "182184060.0", 
                    "available_supply": "47565404.0", 
                    "total_supply": "47565404.0", 
                    "percent_change_1h": "-0.11", 
                    "percent_change_24h": "0.14", 
                    "percent_change_7d": "-3.61", 
                    "last_updated": "1473929943"
                }, 
                {
                    "id": "monero", 
                    "name": "Monero", 
                    "symbol": "XMR", 
                    "rank": "5", 
                    "price_usd": "10.4646", 
                    "price_btc": "0.0171415", 
                    "24h_volume_usd": "4920040.0", 
                    "market_cap_usd": "134987751.0", 
                    "available_supply": "12899466.0", 
                    "total_supply": "12899466.0", 
                    "percent_change_1h": "-0.35", 
                    "percent_change_24h": "-2.47", 
                    "percent_change_7d": "-14.04", 
                    "last_updated": "1473929949"
                } 
            ]';
            $var=json_decode($json);
            foreach ($var as $value) {
               if($value->id=='ripple')
               {
                 echo $value->price_usd;
               }
            }
0

You can use combination of array_search and array_column to get the result.

Note: array_column is only from PHP 5.5 onwards.

$data = json_decode($json_string, true);
// finding the key of ripple
$key = array_search('ripple', array_column($data, 'id'));
echo $result[$key]['price_usd'];

Full Code: (Trimmed JSON string)

<?php

$str = '[
    {
        "id": "bitcoin", 
        "name": "Bitcoin", 
        "symbol": "BTC", 
        "rank": "1", 
        "price_usd": "610.471", 
        "price_btc": "1.0", 
        "24h_volume_usd": "44349800.0", 
        "market_cap_usd": "9687471507.0", 
        "available_supply": "15868848.0", 
        "total_supply": "15868848.0", 
        "percent_change_1h": "0.02", 
        "percent_change_24h": "0.19", 
        "percent_change_7d": "-1.74", 
        "last_updated": "1473931166"
    }, 
    {
        "id": "ethereum", 
        "name": "Ethereum", 
        "symbol": "ETH", 
        "rank": "2", 
        "price_usd": "12.0771", 
        "price_btc": "0.0197825", 
        "24h_volume_usd": "8286810.0", 
        "market_cap_usd": "1014377525.0", 
        "available_supply": "83991813.0", 
        "total_supply": "83991813.0", 
        "percent_change_1h": "-0.02", 
        "percent_change_24h": "1.19", 
        "percent_change_7d": "5.02", 
        "last_updated": "1473931161"
    }, 
    {
        "id": "ripple", 
        "name": "Ripple", 
        "symbol": "XRP", 
        "rank": "3", 
        "price_usd": "0.0059753", 
        "price_btc": "0.00000979", 
        "24h_volume_usd": "1018920.0", 
        "market_cap_usd": "211032409.0", 
        "available_supply": "35317458440.0", 
        "total_supply": "99997205581.0", 
        "percent_change_1h": "-0.26", 
        "percent_change_24h": "1.51", 
        "percent_change_7d": "0.86", 
        "last_updated": "1473931142"
    }
]';

$result = json_decode($str, true);

$key = array_search('ripple', array_column($result, 'id'));
echo $result[$key]['price_usd'];
?>

Output:

0.0059753
Jigar
  • 3,256
  • 1
  • 30
  • 51
0

Since your JSON data is an array of arrays, you could check first if you sub-array has the id 'ripple', and then getting the 'price_usd' like following :

function get_usd_price($data) {
    $price_usd = 0.00;

    foreach( $data as $row ) {
        $id = $row['id'];

        if( $id == 'ripple' && array_key_exists('price_usd', $row) ) {
            $price_usd = $row['price_usd'];
        }
    }

    return $price_usd;
}

$data       =   json_decode($myJSONarray);
$price_usd =    get_usd_price('ripple', $data); // returns 0.00597615 in your example
Anwar
  • 4,162
  • 4
  • 41
  • 62