Php 动态地将数组推入多维数组

Php 动态地将数组推入多维数组,php,arrays,Php,Arrays,我正在尝试为Facebook Messenger SDK动态构建一个数组,并用数据库中的数据填充它 无论我做什么尝试,似乎都无法获得结构正确的数组 我需要的是: $messages = [ "attachment" => [ "type" => "template", "payload" => [ "template_type" =&g

我正在尝试为Facebook Messenger SDK动态构建一个数组,并用数据库中的数据填充它

无论我做什么尝试,似乎都无法获得结构正确的数组

我需要的是:

        $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"]]
                    ]
                ]]
            ]];
我需要根据数据库中的数据在
按钮中创建数据,尝试使用
array\u 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" => [$buttons]
//new array  ^        ^  
正在使用方括号创建一个新数组,以避免它。使用

"buttons" => $buttons

使用
“buttons”=>$buttons
?我不敢相信我没有试过。谢谢,它现在工作了。我很高兴我能帮上忙,快乐编码!