1

I'm trying to dynamically build an array for the Facebook Messenger SDK and populate it with data from a database.

I can't seem to get a properly structured array no matter what I try.

What I need:

        $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"]]
                    ]
                ]]
            ]];

I need to create the data inside buttons based on what I have in the database, tried with array_merge and inserting the array as a string:

        // 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
                        ]
                    ]
                ]]
            ]];

Screenshot showing the differences between correct and incorrect: enter image description here

So basically $buttons are created inside an extra array, how can I get rid of it? Maybe I should change the whole approach?

3
  • 1
    use "buttons" => $buttons ? Commented Jun 30, 2018 at 21:19
  • I can't believe I didn't try that. Thanks, it's working now. Commented Jun 30, 2018 at 21:20
  • i am glad that i could help, happy coding! Commented Jun 30, 2018 at 21:24

1 Answer 1

1

With

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

a new array is being created with square brackets so to avoid it. use

"buttons" => $buttons
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.