0
\$\begingroup\$

It's been a while I have been on here and I hope I still understand the rules😊. I have recently started learning PHP and WordPress for a Whatsapp Chatbot integration. I have created a webhook on my WordPress site that returns a JSON, which is consumed by Google Dialogflow(Chatbot engine).

For example, my webhook returns something like this

{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Text response from webhook"
        ]
      }
    }
  ]
}

To achieve this structure, I have written used this nested associative array

$innerarr= array();
$innerarr[0] = "Text response from webhook";
$newarr = array();
$newarr["fulfillmentMessages"]= array();
$newarr["fulfillmentMessages"][0] = array("text"=>array("text"=>$innerarr));
echo json_encode($newarr);

It works but I'm also thinking, should I

  1. Use a class definition instead that manages the above?
  2. Use the \stdClass object to dynamically create this structure?
  3. Is there a neater way to do this as the dynamic properties will grow depending on the request from the chatbot
  4. Am I overthinking?
\$\endgroup\$
1
  • \$\begingroup\$ This question may be closed for asking for "general best practices". \$\endgroup\$ Mar 29, 2021 at 0:08

2 Answers 2

2
\$\begingroup\$

Your vague sample indicates that you are only pushing one text message into the structure.

  • I see no compelling reason to declare the variables in your sample script.
  • There is also no benefit in creating objects, because json_encode() is going to be using the same syntax when the data is converted to a json string.

I recommend that you do away with the single-use variables and write the necessary structure directly into the json_encode() call.

Code: (Demo with JSON_PRETTY_PRINT for readabilty)

echo json_encode(
         [
             'fulfillmentMessages' => [
                 [
                     'text' => [
                         'text' => [
                             'Text response from webhook',
                         ]
                     ]
                 ]
             ]
         ]
     );

I am unable to advise on "the dynamic properties will grow depending on the request from the chatbot" because you have not been clear about what this data will look like. Asking for "General Best Practices" is off-topic on CodeReview.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Just for completeness, No difference if using assoc arrays or objects. Well there is one exception - empty array vs. empty object. One is encoded as array and the other as object. Unless JSON_FORCE_OBJECT is used.... \$\endgroup\$
    – slepic
    Mar 29, 2021 at 3:26
0
\$\begingroup\$

Do you can use a converter tool to help you understand the conversion structure, ex.: wtools.io. In this case, the PHP array structure for your json is similar that it:

$innerarr = array (
  'fulfillmentMessages' => 
  array (
    0 => 
    array (
      'text' => 
      array (
        'text' => 
        array (
          0 => 'Text response from webhook',
        ),
      ),
    ),
  ),
);

echo json_encode($innerarr);

or, using sort arrays:

$innerarr = [
  'fulfillmentMessages' => [
    0 => [
      'text' => [
        'text' => [
          0 => 'Text response from webhook',
        ],
      ],
    ],
  ],
];

echo json_encode($innerarr);
\$\endgroup\$
1
  • 3
    \$\begingroup\$ You have provided two alternative approaches, but I am not sure that you have provided a review. Why is $innerarr a good variable name for the entire set of data? \$\endgroup\$ Mar 29, 2021 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.