PHP数组Push数据错误:如何正确向inline_keyboard添加元素
解决PHP数组元素追加到指定子数组的问题
你当前的代码错误在于直接给主数组$customKeyboard追加元素,这会在主数组里新增一个独立的索引项,而不是把元素加入到inline_keyboard子数组中。
正确的追加方式
要把新元素添加到inline_keyboard数组里,需要明确指定操作的是这个子数组:
$customKeyboard['inline_keyboard'][] = [["text" => "test", "callback_data" => "test"]];
完整示例代码
// 初始化键盘数组 $customKeyboard = [ "inline_keyboard" => [ [["text" => "back 🔙", "callback_data" => "back"]] ] ]; // 追加新按钮到inline_keyboard $customKeyboard['inline_keyboard'][] = [["text" => "test", "callback_data" => "test"]];
执行后,$customKeyboard的结构会和你预期的完全一致:
$customKeyboard = [ "inline_keyboard" => [ [["text" => "back 🔙", "callback_data" => "back"]], [["text" => "test", "callback_data" => "test"]] ] ];
内容的提问来源于stack exchange,提问作者Arash
相关产品推荐
相关产品推荐

