Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php检索JSON数据的值_Php - Fatal编程技术网

如何使用php检索JSON数据的值

如何使用php检索JSON数据的值,php,Php,我有一个JSON变量,无法获取第二维度的数据。 例如,如何使用php在下面的代码中检索key='TransactionMount'的值 我可以检索transactionid,resultdescription $json = { "Result": { "ResultType": 0, "ResultCode": 0, "ResultDesc": "The service request is processed successfully.

我有一个JSON变量,无法获取第二维度的数据。 例如,如何使用php在下面的代码中检索key='TransactionMount'的值

我可以检索transactionid,resultdescription

$json = {
    "Result": {
        "ResultType": 0,
        "ResultCode": 0,
        "ResultDesc": "The service request is processed successfully.",
        "OriginatorConversationID": "2159-1931249-1",
        "ConversationID": "AG_20190925_0000425ed57b5e1fee9b",
        "TransactionID": "NIP21HAIT2",
        "ResultParameters": {
            "ResultParameter": [{
                "Key": "TransactionReceipt",
                "Value": "NIP21HAIT2"
            }, {
                "Key": "TransactionAmount",
                "Value": 500
            }, {
                "Key": "B2CWorkingAccountAvailableFunds",
                "Value": 350000.00
            }, {
                "Key": "B2CUtilityAccountAvailableFunds",
                "Value": 5134.00
            }, {
                "Key": "TransactionCompletedDateTime",
                "Value": "25.09.2019 18:30:23"
            }, {
                "Key": "ReceiverPartyPublicName",
                "Value": "254708374149 - John Doe"
            }, {
                "Key": "B2CChargesPaidAccountAvailableFunds",
                "Value": -440.00
            }, {
                "Key": "B2CRecipientIsRegisteredCustomer",
                "Value": "Y"
            }]
        },
        "ReferenceData": {
            "ReferenceItem": {
                "Key": "QueueTimeoutURL",
                "Value": "https:\/\/internalsandbox.safaricom.co.ke\/mpesa\/b2cresults\/v1\/submit"
            }
        }
    }
}

首先,使用json\u decode(您的\u json\u字符串,true),然后您可以访问它:

$json = json_decode("your json", true);
$json["Result"]["TransactionID"] or $json["Result"]["ResultDesc"]

关于json_decode的更多信息请参见此处:

首先,您需要使用方法对json字符串进行解码

正如您所要求的,您需要获取
ResultParameters
中的数据,它也是一个数组。所以要获取数组中的数据

echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][0]["Key"]; // Print the Key
echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][0]["Value"]; // Print the Value
因此,在上面的示例中,0是数组中的第一个元素。您必须迭代所有数组元素以获得所有键值对

由于这不是在数组中获取数据的最佳方法,因此可以使用以下方法迭代数据:

for ($i = 0; $i < count($resultArray["Result"]["ResultParameters"]["ResultParameter"]); $i++) {
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Key"];
    echo " ";
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Value"];
    echo "\n";
}

希望这对您有所帮助。

TransactionMount如何?$json[“Result”][“ResultParameters”][“ResultParameters”][“ResultParameters”][1][“Value”]谢谢。但我的大问题是如何从json中检索TransactionMountstring@DenisKipkorir我已经更新了我的答案。谢谢。是的,成功了,我准备好了吗?谢谢Sir@DenisKipkorir很高兴知道这有帮助。答案将用循环的
更新。嗨!要问一个有助于人们帮助您、对未来读者有用的问题,请尝试包含一个简化的JSON块,向我们展示您迄今为止所做的尝试,以及您正在努力的方向。
for ($i = 0; $i < count($resultArray["Result"]["ResultParameters"]["ResultParameter"]); $i++) {
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Key"];
    echo " ";
    echo $resultArray["Result"]["ResultParameters"]["ResultParameter"][$i]["Value"];
    echo "\n";
}
TransactionReceipt NIP21HAIT2
TransactionAmount 500
B2CWorkingAccountAvailableFunds 350000 
B2CUtilityAccountAvailableFunds 5134 
TransactionCompletedDateTime 25.09.2019 18:30:23
ReceiverPartyPublicName 254708374149 - John Doe
B2CChargesPaidAccountAvailableFunds -440
B2CRecipientIsRegisteredCustomer Y