PHP从JSON数组获取值

时间:2018-11-29 05:11:28

标签: php arrays json

我有一个来自金属交易所的JSON数组,我正在尝试从中获取AUD值

JSON在下面

{
    "GoldPrice": {
        "per": "gram",
        "date": "2013-01-23 11:51:48",
        "ARS": {
            "currencyname": "Argentine Peso",
            "bid": "268.9399"
        },
        "AUD": {
            "currencyname": "Australian Dollar",
            "bid": "51.4023"
        },
        "BRL": {
            "currencyname": "Brazilian Real",
            "bid": "110.6755"
        },
        "CAD": {
            "currencyname": "Canadian Dollar",
            "bid": "54.1633"
        },
        "CHF": {
            "currencyname": "Swiss Franc",
            "bid": "50.3933"
        },
        "CNY": {
            "currencyname": "Chinese Yuan Renminbi",
            "bid": "337.5324"
        },
        "COP": {
            "currencyname": "Colombian Peso",
            "bid": "96364.8523"
        },
        "EUR": {
            "currencyname": "Euro",
            "bid": "40.7594"
        },
        "GBP": {
            "currencyname": "British Pound",
            "bid": "34.223"
        },
        "HKD": {
            "currencyname": "Hong Kong Dollar",
            "bid": "420.5594"
        },
        "IDR": {
            "currencyname": "Indonesian Rupiah",
            "bid": "521834.3896"
        },
        "INR": {
            "currencyname": "Indian Rupee",
            "bid": "2911.3477"
        },
        "JPY": {
            "currencyname": "Japanese Yen",
            "bid": "4797.0196"
        },
        "KWD": {
            "currencyname": "Kuwaiti Dinar",
            "bid": "15.2753"
        },
        "MXN": {
            "currencyname": "Mexican Peso",
            "bid": "686.7926"
        },
        "MYR": {
            "currencyname": "Malaysian Ringgit",
            "bid": "164.9691"
        },
        "NZD": {
            "currencyname": "New Zealand Dollar",
            "bid": "64.3396"
        },
        "PEN": {
            "currencyname": "Peruvian Nuevo Sol",
            "bid": "138.6441"
        },
        "PHP": {
            "currencyname": "Philippine Peso",
            "bid": "2203.9583"
        },
        "RUB": {
            "currencyname": "Russian Rouble",
            "bid": "1637.4859"
        },
        "SEK": {
            "currencyname": "Swedish Krona",
            "bid": "354.3917"
        },
        "SGD": {
            "currencyname": "Singapore Dollar",
            "bid": "66.5474"
        },
        "TRY": {
            "currencyname": "Turkish Lira",
            "bid": "96.0565"
        },
        "USD": {
            "currencyname": "United States Dollar",
            "bid": "54.2447"
        },
        "VUV": {
            "currencyname": "Vanuatu Vatu",
            "bid": "4881.8746"
        },
        "ZAR": {
            "currencyname": "South African Rand",
            "bid": "489.4774"
        }
    }
}

我想要的部分是

“ AUD”:{                 “ currencyname”:“澳元”,                 “ bid”:“ 51.4023”             }

我想获取Bid值并将其加载到变量中。

如果有人可以像我尝试的那样提供帮助

$jsonArray = json_decode($json, true);
 echo $jsonArray->GoldPrice->AUD->bid;

但是什么也没显示。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

使用上面的代码,只需删除nginx.conf的{​​{1}}部分。

true标志使json_decode的输出成为一个关联数组,而不是将其保留为对象。使用, true进行访问会遍历对象。 (PHP Manual: json_decode

实际上,您有两个选择。

json_decode

->

我个人通常选择第二种方法。