我有如下代码并更改每个查询。我想通过他们的名字获得价值。
{
"code": null,
"msg": null,
"success": true,
"requestId": "72b214192f8a20e0",
"deviceSn": "2103279315",
"deviceId": 209268286,
"deviceType": "INVERTER",
"deviceState": 1,
"collectionTime": 1696314458,
"dataList": [{
"key": "SN1",
"value": "2103279315",
"unit": null,
"name": "SN"
}, {
"key": "I_T",
"value": "String Single Inverter",
"unit": null,
"name": "Inverter Type"
}, {
"key": "DV1",
"value": "374.90",
"unit": "V",
"name": "DC Voltage PV1"
}]
}
例如;我想获得“DV1”键值(374,90)。这个怎么做?
我无法使用他们的名字访问此值。
使用
foreach
循环数组并检查键是否匹配 DV1
:
$jsonArray = json_decode('{"code": null, ..., "dataList": [{..}]}', true);
foreach ($jsonArray['dataList'] as $dataList) {
if ($dataList['key'] === 'DV1') {
echo $dataList['value'] . PHP_EOL; // 374.90
}
}