Chatfuel将此作为回应的一种方式:
{
"messages": [
{"text": "Welcome to the Chatfuel Rockets!"},
{"text": "What are you up to?"}
]
}我想用我的文本输出类似这样的内容,但是不可能输出具有相同值的键,因为它输出具有最后一个值的第一个键
<?php
$arr = array(array('messages' => array('text' => "Text 1", 'text' => "text
2")));
if ("test" == "test"){
echo json_encode($arr);
}输出:{“消息”:{“文本”:“文本2"}}
如何输出chatfuel所请求的方法?
发布于 2018-05-25 10:21:57
我将使它变得相当详细,这样您就可以看到结构是如何生成的。有一个外部对象,它包含一个"message“属性,它是一个”message“对象数组,每个对象都有一个"text”属性。
V1
$json = new stdClass();
$json->messages = array();
$message = new stdClass();
$message->text = 'Welcome to the Chatfuel Rockets!';
$json->messages[] = $message;
$message = new stdClass();
$message->text = 'What are you up to?';
$json->messages[] = $message;
echo json_encode( $json, JSON_PRETTY_PRINT );V2
$json = array(
'messages' => array(
array(
'text' => 'Welcome to the Chatfuel Rockets!'
),
array(
'text' => 'What are you up to?'
),
)
);
echo json_encode( $json, JSON_PRETTY_PRINT );发布于 2018-05-25 10:32:01
$arr = ['messages' => [['text' => 'Text 1'], ['text' => 'Text 2']]];
echo json_encode($arr, JSON_PRETTY_PRINT);输出:
{
"messages": [
{
"text": "Text 1"
},
{
"text": "Text 2"
}
]
}https://stackoverflow.com/questions/50520528
复制相似问题