如何从 php 中的嵌套 json 获取值
How to get values from nested json in php
我有这个 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'];
然而,这并没有达到我的预期。我需要做什么才能访问交易部分中的数据?
$content['data']['transactions']
里面的元素比较多,所以是数组
尝试这样的事情:
$rig_detail= $content['data']['transactions'][0]['id'];
我有这个 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'];
然而,这并没有达到我的预期。我需要做什么才能访问交易部分中的数据?
$content['data']['transactions']
里面的元素比较多,所以是数组
尝试这样的事情:
$rig_detail= $content['data']['transactions'][0]['id'];