-
-
Notifications
You must be signed in to change notification settings - Fork 955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamical array of callback's data for use in InlineKeyboard
#533
Comments
I thinks must be use |
I find solution in this: #496 ticket. Solution 1: return new InlineKeyboard(...$keyboard_buttons); Solution 2: $inline_keyboard = new InlineKeyboard([]);
foreach($keyboard_buttons as $keyboard_button) {
call_user_func_array([$inline_keyboard, 'addRow'], $keyboard_button);
}
return $inline_keyboard; And completely simplified: $inline_keyboard = new InlineKeyboard([]);
$keyboard_buttons = [];
$n = 0;
foreach ($menus as $key => $value) {
$n++;
$keyboard_buttons[] = new InlineKeyboardButton([
'text' => $value,
'callback_data' => $key,
]);
if ($n % 3 == 0 || count($menus) == $n) {
call_user_func_array([$inline_keyboard, 'addRow'], $keyboard_buttons);
$keyboard_buttons = [];
}
}
return $inline_keyboard; Thanks to @jacklul and @noplanman |
Nice solution! I've done something similar to this in the past, here my solution to give you some inspiration 😇 $keyboard_buttons = [];
foreach ($menus as $callback_data => $text) {
$keyboard_buttons[] = new InlineKeyboardButton([
'callback_data' => $callback_data,
'text' => $text,
]);
// Or if you like 1-liners:
// $keyboard_buttons[] = new InlineKeyboardButton(compact('callback_data', 'text'));
}
$keyboard_rows = array_chunk($keyboard_buttons, 3);
$data['reply_markup'] = new InlineKeyboard(...$keyboard_rows); and just for the sake of it, a tiny version: array_walk($menus, function (&$text, $callback_data) {
$text = new InlineKeyboardButton(compact('callback_data', 'text'));
});
$data['reply_markup'] = new InlineKeyboard(...array_chunk($choices, 3)); Note: You'll need at least PHP 5.6 for the argument unpacking to work. |
wow that's perfect. |
I know about
InlineKeyboard
class, can get each inline keyboard buttons as separated argument.Now I have
$menus
dynamical array of callback's data.I want to read all the data with
foreach
and make category 3 each for every line I create.Be like this:
Sample code:
The text was updated successfully, but these errors were encountered: