简体   繁体   中英

Set values to a JSON object array in PHP

I want to set a string value to the text array inside the text object which is inside the fulfillmentMessages JSON array using PHP. I am currently trying with this code, but have failed.

$response->fulfillmentMessages[text]->text = "Hello ";
echo json_encode($response);

The JSON I am getting with this:

{
  "responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
  "queryResult": {
    "queryText": "yes",
    "action": "send.first.question",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            ""
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/quizbot-90a24/agent/sessions/9c08dfd274-7ece-19fe-c984-f5cd5ef135c7/contexts/expressname-followup",
        "lifespanCount": 1,
        "parameters": {
          "userName": "saliya",
          "userName.original": "saliya"
        }
      },
      {
        "name": "projects/quizbot-90a24/agent/sessions/9c08fdvfsd4-7ece-19fe-c984-f5cd5ef135c7/contexts/sessionusername",
        "lifespanCount": 4,
        "parameters": {
          "userName": "saliya",
          "userName.original": "saliya"
        }
      }
    ],
    "intent": {
      "name": "projects/quizbot-90agd24/agent/intents/063ed465-0ca1-4d94-bf7c-5b7ac4c31f6f",
      "displayName": "expressName - yes"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "end_conversation": true
    },
    "languageCode": "en"
  }
}

This is the result I intend to take:

"fulfillmentMessages": [
  {
    "text": {
      "text": [
        "Hello"
      ]
    }
  }

your indentation threw me for a second. this will work

$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";

I didnt notice that fulfillmentMessages was a property of queryResult .

You're trying to manipulate an array item in nested object and array, so you need to change it to.

$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";

Example:

<?php
$response = json_decode('{
    "responseId": "b19f0045-830e-4d12-a57e-07cdfd55f",
    "queryResult": {
        "queryText": "yes",
        "action": "send.first.question",
        "parameters": {},
        "allRequiredParamsPresent": true,
        "fulfillmentMessages": [{
            "text": {
                "text": [
                    ""
                ]
            }
        }]
    }
}');

$response->queryResult->fulfillmentMessages[0]->text->text[0] = "Hello";

print_r($response);

Result:

stdClass Object
(
    [responseId] => b19f0045-830e-4d12-a57e-07cdfd55f
    [queryResult] => stdClass Object
        (
            [queryText] => yes
            [action] => send.first.question
            [parameters] => stdClass Object
                (
                )

            [allRequiredParamsPresent] => 1
            [fulfillmentMessages] => Array
                (
                    [0] => stdClass Object
                        (
                            [text] => stdClass Object
                                (
                                    [text] => Array
                                        (
                                            [0] => Hello
                                        )

                                )

                        )

                )

        )

)

https://3v4l.org/SodvM

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM