简体   繁体   English

PHP数组大括号而不是方括号

[英]PHP array curly brackets instead of square brackets

I'm attempting to output curly brackets from my array like this: 我试图像这样从我的数组输出大括号:

"data":{facebook":{"message"}},

but I keep getting square brackets: 但我不断得到方括号:

"data":{"facebook":["message"]}

Here is my code: 这是我的代码:

$output["contextOut"] = array(array("name" => "$next-context", "parameters" =>
array("param1" => $param1value, "param2" => $param2value)));
$output["speech"] = $outputtext;
$output["data"] = array("facebook" => array("message"));
$output["displayText"] = $outputtext;
$output["source"] = "index.php";
ob_end_clean();
echo json_encode($output);

and this is my json encoded output: 这是我的json编码的输出:

{"contextOut":[{"name":"buy-context","parameters":{"param1":null,"param2":null}}],"speech":"msg","data":{"facebook":["message"]},"displayText":"msg","source":"index.php"}

How do I obtain the curly brackets instead of the square brackets? 如何获取大括号而不是方括号? Thanks in advance for any help. 在此先感谢您的帮助。

As Paul Crovella said, your stated goal is invalid JSON. 正如Paul Crovella所说,您声明的目标是无效的JSON。

Your valid options are for the facebook property to directly contain the message string: 您的有效选项是使facebook属性直接包含消息字符串:

{
    "data":{"facebook":"message"},
}

(note I've added the outer { and } missing from your question) ...in which case you want: (请注意,我在您的问题中添加了外面的{} ...在这种情况下,您需要:

$output["data"] = array("facebook" => "message");

Or you can make facebook refer to an object with a message property that has a value, like this: 或者,您可以使facebook引用具有具有值的message属性的对象,如下所示:

{
    "data":{"facebook":{"message":"value"}},
}

by doing this: 通过做这个:

$output["data"] = array("facebook" => array("message" => "value"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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