2017-09-07 78 views
-1

我正在玩弄Slack /命令, 需要发回松弛的给定json看起来像这样。将php数组转换为json

{ 
"text" : "hello world", 
    "attachments": [{ 
        "text" : " this is information" 

       }] 
} 

我想通过这种方式复制这个。

$data = array(
     "text" => "hello world", 
     "attachments" => array(
      "text" => "this is information", 
      "author_name" => "masnad" 
     ) 
    ); 

$this->output->set_content_type('application/json'); 
return $this->output->set_output(json_encode($data)); 

我只是不能让方括号工作,所以松懈理解。

+3

请求JSON是错误的。你需要大括号。 –

+0

@RahulMeshram我试过json_encode,不起作用,你不能给这个消极点。 –

+0

我没有。 Huhhh。相反,我把你的问题的重复收回了!你怎么能!请 – rahulsm

回答

1

只是包装每个附件的阵列

$data = array(
    "text" => "hello world", 
    "attachments" => array(
     array("text" => "this is information"), 
     array("text" => "this is another information"), 
    ) 
); 

随着现代PHP中,和可读性的原因,你应该使用数组的方括号表示法:

$data = [ 
    "text" => "hello world", 
    "attachments" => [ 
     [ "text" => "this is information" ], 
     [ "text" => "this is another information" ], 
    ] 
];