如何在php中为json数组添加数组?

时间:2018-01-28 05:19:58

标签: arrays json

我试图在php中将数组转换为json格式, 我想做的格式就像



 {
      "attachment": {
        "type": "template",
        "payload": {
          "template_type": "list",
          "top_element_style": "large",
          "elements": [
            {
              "title": "Classic White T-Shirt",
              "image_url": "http://petersapparel.parseapp.com/img/item100-thumb.png",
              "subtitle": "Soft white cotton t-shirt is back in style",
              "buttons": [
                {
                  "type": "web_url",
                  "url": "https://petersapparel.parseapp.com/buy_item?item_id=101",
                  "title": "Buy Item"
                }
              ]
            },




这是我要转换的代码,



       $message = ["attachment"=>["type"=>"template","payload"=>["template_type"=>"list","top_element_style"=>"large","elements"=>["title"=>"test","image_url"=> "https://cdn-images-1.medium.com/max/375/0*sepg7sXXe3zcmGvZ.png","subtitle"=>"Soft white cotton t-shirt is back in style" ]]]]; 



 这给我这样的结果 enter image description here 我还有一些步骤来添加按钮类型,网址和标题,任何人都可以指导我。

1 个答案:

答案 0 :(得分:0)

如果要将数组转换为json,请使用json_encode。如果要将json转换为数组或对象,请使用json_decode。假设您要添加新阵列。

$message['attachment']['payload']['elements']['buttons'] = array();