2017-09-07 79 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" ], 
    ] 
];