0

I have a JSON array from a metals exchange, and I am trying to get the AUD value from it

The JSON is below

{
    "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"
        }
    }
}

The part I would like is

"AUD": { "currencyname": "Australian Dollar", "bid": "51.4023" },

I would like to get the Bid value and load it into a variable.

If someone could please assist as I have tried

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

But nothing is displayed.

Any help would be appreciated.

1
  • You passed second parameter true to json_decode it's converted to associative array so you should call like this echo $jsonArray['GoldPrice']['AUD']['bid']; OR you can remove second parameter true Commented Nov 29, 2018 at 5:17

1 Answer 1

3

With the code you have above, simply remove the , true portion of your json_decode.

The true flag makes the output of json_decode into an associative array, instead of leaving it as an object. Accessing using -> traverses through an object. (PHP Manual: json_decode)

So really, you have two options.

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

or

$jsonArray = json_decode($json, true);
echo $jsonArray['GoldPrice']['AUD']['bid'];

Personally, I generally go with the second option.

Sign up to request clarification or add additional context in comments.

2 Comments

You are awesome. Thank you. I always get stuck on arrays. This has helped a lot.
Thank you kind internet sir! Arrays can be really confusing, but just picture them as folders - each time you go a level deeper, you've moving into another child folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.