我试图在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" ]]]];

答案 0 :(得分:0)
如果要将数组转换为json,请使用json_encode
。如果要将json转换为数组或对象,请使用json_decode
。假设您要添加新阵列。
$message['attachment']['payload']['elements']['buttons'] = array();