2017-09-07 2 views
-1

슬랙/명령을 가지고 놀고 있습니다. 슬랙에 되돌려 보내야 할 주어진 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

을 . 중괄호가 필요합니다. –

+0

@RahulMeshram json_encode를 시도했는데 작동하지 않습니다. 그저 부정적인 점은 없습니다. –

+0

나는 그렇지 않았다. 응. 나는 당신의 질문에 대신 내 복제를 철회! 어떻게 그럴 수있어? 제발 – rahulsm

답변

1

그냥 현대적인 PHP와 배열

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

내의 각 첨부 파일을 포장하고, 가독성을 위해, 당신은 배열 대괄호 표기법을 사용해야합니다 : 요청 된 JSON이 잘못

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