代码之家  ›  专栏  ›  技术社区  ›  Ricardo

动态地将数组推送到多维数组中

  •  1
  • Ricardo  · 技术社区  · 6 年前

    我正在尝试为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
                            ]
                        ]
                    ]]
                ]];
    

    enter image description here

    $buttons

    1 回复  |  直到 6 年前
        1
  •  1
  •   Pavneet_Singh    6 年前

    "buttons" => [$buttons]
    //new array  ^        ^  
    

    正在创建一个带有方括号的新数组,以避免出现这种情况。使用

    "buttons" => $buttons