2016-09-25 80 views
-1

我试图输出大括号从我的数组是这样的:PHP数组大括号而不是方括号

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

,但我不断收到方括号:

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

这里是我的代码:

$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); 

,这是我的JSON编码输出:

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

如何获得大括号而不是方括号?预先感谢您的帮助。

+2

你所需的输出不是有效的JSON。 –

+2

'{}'用于__object__。 –

+0

请参阅[JSON语法](http://www.json.org/) – RiggsFolly

回答

2

正如Paul Crovella所说,你声明的目标是无效的JSON。

你有效的选项都为facebook属性直接包含消息字符串:

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

(注意:我已经添加了外{}从你的问题遗漏) ...在这种情况下,你想:

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

或者你可以让facebook参考的对象与具有价值,像这样的message性质:

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

通过这样做:

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