0

I'm trying to get a data from a JSON.

The JSON looks like this:

{
  "object": "balance",
  "available": [
    {
      "currency": "aud",
      "amount": 0,
      "source_types": {
        "card": 0
      }
    }
  ],
  "livemode": false,
  "pending": [
    {
      "currency": "aud",
      "amount": 37706,
      "source_types": {
        "card": 37706
      }
    }
  ]
}{
  "object": "balance",
  "available": [
    {
      "currency": "aud",
      "amount": 0,
      "source_types": {
        "card": 0
      }
    }
  ],
  "livemode": false,
  "pending": [
    {
      "currency": "aud",
      "amount": 37706,
      "source_types": {
        "card": 37706
      }
    }
  ]
}{
  "object": "balance",
  "available": [
    {
      "currency": "aud",
      "amount": 0,
      "source_types": {
        "card": 0
      }
    }
  ],
  "livemode": false,
  "pending": [
    {
      "currency": "aud",
      "amount": 37706,
      "source_types": {
        "card": 37706
      }
    }
  ]
}

All I need from there is the available balance.

I tried this code but the output of this code is just this {{{

This is my code:

$amount = $balanceArr['balance']['available'][0]['amount'];

echo $amount;

Note that the $balanceArr is just the JSON.

Could someone please advice on this issue?

Any help would be appreciated.

Thanks in advance.

David Hope
  • 1,426
  • 4
  • 21
  • 50

2 Answers2

1

use json_decode to make php assoc array from json string http://php.net/manual/ru/function.json-decode.php

Max Deepfield
  • 349
  • 1
  • 7
1

you need to json_decode the output, by default it will be objects, if you do

$balanceArr = json_decode($balanceArr, true);

above the code you posted, that will likely work

Your json is incorrectly formed also

$balanceArr[0]['available'][0]['amount']

Will work if your json string is valid (needs some commas and an array wrapping the whole thing)

exussum
  • 18,275
  • 8
  • 32
  • 65