我正在尝试为Facebook信使SDK动态构建一个数组,并用数据库中的数据填充它。
无论我如何尝试,我似乎都无法获得正确的结构化数组。
我需要:
$messages=[
“有效载荷”=>。[
“image_url”=>$row[“image_url”],
“按钮”=>。[[
“url”=>“www.google.com”,
“title”=>“开始聊天”,
]]
如果(!空($row['button1_type'])。{
“title”=>$row['button1_title'],
如果(!空($row['button2_type'])。{
“title”=>$row['button2_title'],
}
$buttons=“\”type\“=>”..$row['button2\u type'],“\”url\“=>”..$row['button2\u url'],
“template_type”=>“通用”,
“item_url”=>$row[“item_url”],
“subtitle”=>$row[“subtitle”],
$按钮
]
显示正确和错误之间差异的屏幕截图:
所以基本上,$buttons是在一个额外的数组中创建的,我如何才能摆脱它?也许我应该改变整个方法?
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [[
"type" => "web_url",
"url" => "www.google.com",
"title" => "View Website"],
["type" => "postback",
"title" => "Start Chatting",
"payload" => "start"]]
]
]]
]];
buttons
根据我在数据库中的数据,尝试array_merge
// Created arrays
if (!empty($row['button1_type'])) {
$buttons[] = array("type" => $row['button1_type'],"url" => $row['button1_url'],
"title" => $row['button1_title'],
"payload" => $row['button1_payload']);
}
if (!empty($row['button2_type'])) {
$buttons[] = array("type" => $row['button2_type'],"url" => $row['button2_url'],
"title" => $row['button2_title'],
"payload" => $row['button2_payload']);
}
// In the case when I had array_merge - $buttons were actually named as $buttons1 and $buttons2
$buttons = array_merge($buttons1, $buttons2);
// Tried to add it as a string
if (!empty($row['button2_type'])) {
$buttons = "\"type\" => ".$row['button2_type'].",\"url\" => .".$row['button2_url'].",
\"title\" => ".$row['button2_title'].",
\"payload\" => ".$row['button2_payload'];
}
$messages = [
"attachment" => [
"type" => "template",
"payload" => [
"template_type" => "generic",
"elements" => [[
"title" => $row['title'],
"item_url" => $row['item_url'],
"image_url" => $row['image_url'],
"subtitle" => $row['subtitle'],
"buttons" => [
$buttons
]
]
]]
]];
$buttons