PHP - декодирование json многомерного массива с циклом foreach - PullRequest
0 голосов
/ 28 апреля 2020

Я пытаюсь получить определенное значение, например, $ 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";
                }
            }
        }
    }

Однако я получаю пустой вывод , API ответил файлом xml, который я закодировал в json. Я так долго пытался это выяснить, и понятия не имею, что происходит. Может кто-нибудь помочь? Спасибо

{
    "@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": {}
    }
    }

1 Ответ

1 голос
/ 28 апреля 2020

Вам не нужно использовать foreach l oop, потому что значения не массив, а объекты.

Вы можете получить значение просто

$value = json_decode($json)->receipt->total->normalizedValue . '\n';

из c $json должен быть расшифрован как php json_decode($json)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...