[英]PHP - decoding json multidimensional array with foreach loop
Im attempting to retrieve a certain value, for instance, $json -> receipt -> total -> normalizedValue using我试图检索某个值,例如, $json -> 收据 -> 总计 -> normalizedValue使用
foreach ($json->data as $data) {
foreach ($data->receipt as $receipt) {
foreach($receipt->payment as $payment) {
foreach($payment->value as $value) {
foreach($value->normalizedValue as $normalizedValue) {
echo json_encode($normalizedValue) . "\n";
}
}
}
}
However, im getting a blank output.但是,我得到一个空白的 output。 An API responded with an xml file which I encoded into json.
一个 API 响应了一个 xml 文件,我将其编码为 json。 I've spent so long trying to figure this out, and have no clue whats going on.
我花了很长时间试图弄清楚这一点,但不知道发生了什么。 Can someone help?
有人可以帮忙吗? Thanks
谢谢
{
"@attributes": {
"count": "1"
},
"receipt": {
"@attributes": {
"currency": "USD",
"rotation": "RT_Clockwise"
},
"vendor": {
"@attributes": {
"confidence": "0.000",
"isSuspicious": "true"
},
"name": {
"recognizedValue": {
"text": "SEATTLE"
}
},
"fullAddress": {
"text": {}
},
"address": {
"text": {}
},
"phone": {
"@attributes": {
"confidence": "70",
"isSuspicious": "true"
},
"normalizedValue": "2066219777",
"recognizedValue": {
"text": "206\n621\n9777"
}
},
"purchaseType": "GasolineStation",
"city": {
"normalizedValue": "Seattle",
"recognizedValue": {
"text": "SEATTLE"
}
},
"zip": {
"@attributes": {
"confidence": "99",
"isSuspicious": "false"
},
"normalizedValue": "98134",
"recognizedValue": {
"text": "98134"
}
},
"administrativeRegion": {
"normalizedValue": "WA",
"recognizedValue": {
"text": "WA"
}
}
},
"date": {
"@attributes": {
"confidence": "100",
"isSuspicious": "false"
},
"normalizedValue": "2020-04-02",
"recognizedValue": {
"text": "04/02/20"
}
},
"time": {
"@attributes": {
"confidence": "100",
"isSuspicious": "false"
},
"normalizedValue": "14:17:49",
"recognizedValue": {
"text": "14:17:49"
}
},
"total": {
"@attributes": {
"confidence": "34",
"isSuspicious": "true"
},
"normalizedValue": "150.00",
"recognizedValue": {
"text": "150\n00"
}
},
"tax": {
"@attributes": {
"total": "false"
},
"normalizedValue": "0.00",
"recognizedValue": {
"text": "0.00"
}
},
"payment": {
"@attributes": {
"type": "Card",
"cardType": "AmericanExpress"
},
"value": {
"normalizedValue": "150.00",
"recognizedValue": {
"text": "150\n00"
}
},
"cardNumber": {
"normalizedValue": "XXXXXXXXXXXX2467",
"recognizedValue": {
"text": "X2467"
}
}
},
"recognizedItems": {
"@attributes": {
"count": "1"
},
"item": {
"@attributes": {
"index": "1"
},
"name": {
"@attributes": {
"confidence": "0",
"isSuspicious": "true"
},
"text": "Prepay CA#11"
},
"count": {
"@attributes": {
"confidence": "19",
"isSuspicious": "true"
},
"normalizedValue": "1.000"
},
"total": {
"@attributes": {
"confidence": "85",
"isSuspicious": "false"
},
"normalizedValue": "150.00",
"recognizedValue": {
"text": "150.00"
}
},
"recognizedText": {},
"amountUnits": "unit"
}
},
"country": {
"@attributes": {
"confidence": "100",
"isSuspicious": "false"
},
"normalizedValue": "USA"
},
"recognizedText": {}
}
}
You don't need use foreach loop because the values are not an array but objects.您不需要使用 foreach 循环,因为值不是数组而是对象。
You can getting value simple您可以获得简单的价值
$value = json_decode($json)->receipt->total->normalizedValue . '\n';
ofc $json
must be decoded as php by json_decode($json)
ofc
$json
必须被json_decode($json)
解码为 php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.