dongmie3526 2014-05-08 13:11
浏览 40

超级嵌套的json数组

Please help me pull this into a php variable... I have the following json decoded code:

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

I want to bring out the balance into a php variable and am not sure how to.

when I do

foreach ($accounts as $a)

I can echo $a->account_id just fine - but when I try to echo $a->balances->balance it does not give me the 390.50 for the result...

any kind of help I can get on this is great appreciated!

  • 写回答

1条回答 默认 最新

  • dongmaopan5738 2014-05-08 13:17
    关注

    In your json, balance is also an array so you need to have one more loop to traverse balances objects like:

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)