超级嵌套的json数组

时间:2014-05-08 13:11:56

标签: php arrays json

请帮我把它变成一个php变量......我有以下json解码的代码:

  "account_id":12345,
  "name":"Example account",
  "state":"active",
     "balances":[
        {
          "currency":"USD",
          "balance":390.50,
          "incoming_pending_amount":635.30,
          "outgoing_pending_amount":210.00,
          "reserved_amount":0,
          "disputed_amount":0,
          "withdrawal_period":"daily",
          "withdrawal_next_time":1370112217,
          "withdrawal_bank_name":"WellsFargo XXXXX3102"
        }

我想把余额带到一个php变量中,我不知道该怎么做。

当我做的时候

foreach ($accounts as $a)

我可以回复$ a-> account_id就好 - 但是当我尝试回复$ a-> balances->余额时,它不会给我390.50的结果......

我能得到的任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:2)

在你的json中,balance也是一个数组,因此你需要再有一个循环来遍历balances个对象,如:

foreach($accounts as $a){
    echo $a->account_id;
    echo $a->name;
    echo $a->stat;

    foreach($a->balances as $b) //loop if you want to print all balances
    {
       echo $b->currency;
       echo $b->balance;
       ...
       ...            
    } 

    // echo $a->balances[0]->balance;  //a single statement if you want to access just first balance object
}