I have an array:
$data["messages"][] = array("chatId" => $chat_id, "type" => "text", "to" => $username, "body" => "success");
When print_r the array it looks like this:
Array ( [messages] => Array ( [0] => Array ( [chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success ) ) )
But when I try to append the new key value to the end like this:
$arr_keyboards[] = array(
"type" => "suggested",
"responses" => array(
array("type" => "text", "body" => "Yes"),
array("type" => "text", "body" => "No")
)
);
$data["messages"]["keyboards"] = $arr_keyboards;
I end up with this:
Array ( [messages] => Array ( [0] => Array ( [chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success ) [keyboards] => Array ( [0] => Array ( [type] => suggested [responses] => Array ( [0] => Array ( [type] => text [body] => Yes ) [1] => Array ( [type] => text [body] => No ) ) ) ) ) )
Notice the new key value "keyboards" => array(... gets added AFTER the previous array gets closed. What I want it to look like is:
Array ( [messages] => Array ( [0] => Array ( [chatId] => d3a611401de00e8c8b3e225a7cf95484033f57ea18c442249ee9b5bcb63c9a96 [type] => text [to] => pitashi [body] => success [keyboards] => Array ( [0] => Array ( [type] => suggested [responses] => Array ( [0] => Array ( [type] => text [body] => Yes ) [1] => Array ( [type] => text [body] => No ) ) ) ) )
In the end I'm json encoding it. Here is a screenshot of how it looks and where I need it to be https://www.evernote.com/l/AETNv5OhBI1HtLzqXalq-BCfOIwz4U0jlWw and here is a screenshot of ultimately how I want it to look https://www.evernote.com/l/AETRjOREKvBHLpXyh3NdZdGAryyxO2rIiwg
I'm dumb. Thanks for the help.
Try this:
$data["messages"][] = array(
"chatId" => $chat_id,
"type" => "text",
"to" => $username,
"body" => "success"
);
$arr_keyboards = array(
"type" => "suggested",
"responses" => array(
array("type" => "text", "body" => "Yes"),
array("type" => "text", "body" => "No")
)
);
foreach ($data["messages"] as $message) {
$message["keyboards"] = $arr_keyboards;
}
echo json_encode($message);
Hope this helps.
You are trying to add the $arr_keyboards
array into the first element of the $data["messages"]
array.
Change the " crucial " line of code as following:
$data["messages"][0]["keyboards"] = $arr_keyboards;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.