【问题标题】:How to get values from nested json in php [duplicate]如何从php中的嵌套json中获取值[重复]
【发布时间】:2021-09-07 12:54:10
【问题描述】:

我有这个 json 文件

{
"success": true,
"data": {
    "total": "2",
    "returned": 2,
    "start": 0,
    "limit": 10,
    "transactions": [
        {
            "id": "16393567",
            "type": "Credit",
            "currency": "BTC",
            "amount": "0.00019449",
            "when": "2021-09-03 10:27:48",
            "rental": "3411209",
            "rig": "205363",
            "status": "Pending",
            "pending_seconds": "716202"
        },
        {
            "id": "16377905",
            "type": "Credit",
            "currency": "BTC",
            "amount": "0.00000203",
            "when": "2021-09-01 11:42:47",
            "rental": "3408621",
            "rig": "205363",
            "status": "Cleared",
            "pending_seconds": 0
        }
    ]
}

}

我可以使用此代码获取数据下的值

 $jsonCont = file_get_contents('temp.json');    
 $content = json_decode($jsonCont, true);

 $rig_detail= $content['data']['total'];
 $rig_detail= $content['data']['returned'];
 $rig_detail= $content['data']['start'];
 $rig_detail= $content['data']['limit'];

我的问题存在于我尝试从我尝试过的“交易”中获取数据的地方

 $rig_detail= $content['data']['transactions']['id'];

然而,这并没有给我我所期望的。我需要做什么才能访问交易部分中的数据?

【问题讨论】:

  • $rig_detail= $content['data']['transactions'][0]['id']

标签: php json decode


【解决方案1】:

$content['data']['transactions'] 的元素比较多,所以是一个数组。

试试这样的:

 $rig_detail= $content['data']['transactions'][0]['id'];

【讨论】:

  • 谢谢。这正是我想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2019-07-31
  • 1970-01-01
  • 2016-03-14
  • 1970-01-01
相关资源
最近更新 更多