0

I have a json below but I am struggling to get the values for the keys e.g. TransactionAmount and ReceiverPartyPublicName.

Kindly someone help. Thank you.

{
  "ResultType": "0",
  "ResultCode": "0",
  "TransactionID": "LIB76ANQYD",
  "ResultParameters": {
    "ResultParameter": [
      {
        "Key": "TransactionAmount",
        "Value": "750"
      },
      {
        "Key": "TransactionReceipt",
        "Value": "LIB76ANQYD"
      },
      {
        "Key": "ReceiverPartyPublicName",
        "Value": "345706611796 - PETER Parr"
      }
    ]
  },
  "ReferenceData": {
    "ReferenceItem": {
      "Key": "QueueURL",
      "Value": "http://xxxxxx"
    }
  }
}
Bob Mwenda
  • 89
  • 1
  • 9
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Don't Panic Sep 11 '17 at 18:50

1 Answers1

1

You can use json_decode to iterate over your array checking for the correct Key then you've got the reference to the Value

Demo: https://3v4l.org/CaPWL

<?php

$json = <<<JSON
{
  "ResultType": "0",
  "ResultCode": "0",
  "TransactionID": "LIB76ANQYD",
  "ResultParameters": {
    "ResultParameter": [
      {
        "Key": "TransactionAmount",
        "Value": "750"
      },
      {
        "Key": "TransactionReceipt",
        "Value": "LIB76ANQYD"
      },
      {
        "Key": "ReceiverPartyPublicName",
        "Value": "345706611796 - PETER Parr"
      }
    ]
  },
  "ReferenceData": {
    "ReferenceItem": {
      "Key": "QueueURL",
      "Value": "http://xxxxxx"
    }
  }
}
JSON;

foreach (json_decode($json, true)['ResultParameters']['ResultParameter'] as $parameter) {
    if ($parameter['Key'] === 'TransactionAmount') {
        echo 'TransactionAmount: ' . $parameter['Value'] . PHP_EOL;
    }
    if ($parameter['Key'] === 'ReceiverPartyPublicName') {
        echo 'ReceiverPartyPublicName: ' . $parameter['Value'] . PHP_EOL;
    }
}
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167