如何从PHP中的JSON文件中获取特定条目

时间:2016-09-15 09:14:50

标签: php json

我有this site's输出,这给了我750+山寨币的价值。

我得到输出,并将其解码为new Date().getTime();

让我们将其简化为:

$data

我如何得到[ { "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" }, ] 让我们说,涟漪?我知道我可以price_usd,但信息是基于排名。排名发生变化。如何在不使用$data[3]['price_usd']的情况下检索涟漪值?我试过[1],但无济于事。

我在stackexchange上搜索了这个问题的答案,但是人们只告诉我到目前为止我所知道的。不是如何找到特定条目的值。

此致

8 个答案:

答案 0 :(得分:1)

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

var_dump($price);

答案 1 :(得分:1)

假设你说你有数组中提到的所需硬币的列表,这是json序列的任何序列。然后,而不是每次检查整个json数据。我已检查过,如果数组中的任何id与json id匹配,则选择price_usd。只需遍历json一次就能获得最佳效果。

<?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>";
    }
}
?>

结果 -

bitcoin = 610.398
nexus = 0.0356638
ambercoin = 0.0104771
zombiecoin = 0.000121343

答案 2 :(得分:0)

您可以使用以下功能:

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

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

   return $price;
}

然后你可以将它用于这样的涟漪案。

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

希望这有帮助。

答案 3 :(得分:0)

使用我的自定义函数生成关联数组

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;
        }

$data数组和Keyfield作为id

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

然后您可以将结果作为$data['bitcoin']['price_usd']访问,而无需担心元素的等级。

答案 4 :(得分:0)

这是一种方法:

$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;
    }
}

请注意,您需要从json_decode的最后一项删除尾随逗号才能正常工作。

希望这有帮助

答案 5 :(得分: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;
               }
            }

答案 6 :(得分:0)

您可以使用array_searcharray_column的组合来获得结果。

注意: array_column仅来自 PHP 5.5 以后。

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

完整代码:(修剪JSON字符串)

<?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'];
?>

输出:

0.0059753

答案 7 :(得分:0)

由于您的JSON数据是一个数组数组,因此您可以先检查子数组是否具有id&#39; ripple&#39;,然后获取&#39; price_usd&#39;如下:

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