数组键索引带有数字

时间:2018-03-29 10:27:12

标签: php variables foreach syntax syntax-error

我正在处理下面表格中的数组:

[
    {
        "id": "bitcoin",
        "name": "Bitcoin",
        "symbol": "BTC",
        "rank": "1",
        "price_usd": "7598.02",
        "price_btc": "1.0",
        "24h_volume_usd": "5106040000.0",
        "market_cap_usd": "128754238591",
        "available_supply": "16945762.0",
        "total_supply": "16945762.0",
        "max_supply": "21000000.0",
        "percent_change_1h": "0.01",
        "percent_change_24h": "-5.69",
        "percent_change_7d": "-14.2",
        "last_updated": "1522317867",
        "price_eur": "6162.0702002",
        "24h_volume_eur": "4141049500.4",
        "market_cap_eur": "104420975040"
    },
    {
        "id": "ethereum",
        "name": "Ethereum",
        "symbol": "ETH",
        "rank": "2",
        "price_usd": "415.793",
        "price_btc": "0.0552943",
        "24h_volume_usd": "1521500000.0",
        "market_cap_usd": "40949398071.0",
        "available_supply": "98485059.0",
        "total_supply": "98485059.0",
        "max_supply": null,
        "percent_change_1h": "-1.02",
        "percent_change_24h": "-9.97",
        "percent_change_7d": "-24.69",
        "last_updated": "1522317854",
        "price_eur": "337.21228093",
        "24h_volume_eur": "1233951715.0",
        "market_cap_eur": "33210371330.0"
    }
]


现在我想在FOREACH LOOP中使用PHP获取此数组,但是当我输入此代码时会抛出语法错误

**<?php echo $price_usd = $myVariable->24h_volume_usd ?>**

因为任何变量都不以数字开头。如何在FOREACH LOOP的帮助下获得 24h_volume_usd 值。 帮助我。


提前谢谢。

1 个答案:

答案 0 :(得分:2)

你可以试试这个:

$data = json_decode($dataStr, true); //When TRUE, returned objects will be converted into associative arrays. 
foreach($data as $val){
    echo "\n".$val['24h_volume_eur'];
}

Demo