Skip to content
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

Closed
NabiKAZ opened this issue Jun 9, 2017 · 4 comments
Closed

Dynamical array of callback's data for use in InlineKeyboard #533

NabiKAZ opened this issue Jun 9, 2017 · 4 comments

Comments

@NabiKAZ
Copy link
Contributor

NabiKAZ commented Jun 9, 2017

I know about InlineKeyboard class, can get each inline keyboard buttons as separated argument.

new InlineKeyboard($keyboard_buttons1, $keyboard_buttons2, ...);

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:
image

Sample code:

$menus = [
	'x1'=>'y1',
	'x2'=>'y2',
	'x3'=>'y3',
	'x4'=>'y4',
	'x5'=>'y5',
	'x6'=>'y6',
	'x7'=>'y7',
];
$keyboard_buttons = [];
$keyboard_buttons_line = [];
$n = 0;
foreach ($menus as $key => $value) {
	$n++;
	$keyboard_buttons_line[] = [
		'text'          => $value,
		'callback_data' => $key,
	];
	if ($n % 3 == 0 || count($menus) == $n) {
		$keyboard_buttons[] = $keyboard_buttons_line;
		$keyboard_buttons_line = [];
	}
}

echo "<pre>";
print_r(new InlineKeyboard($keyboard_buttons[0], $keyboard_buttons[1], $keyboard_buttons[2])); //THIS HAVE BUG !!! (I don't know how many argument must be set.)
@NabiKAZ
Copy link
Contributor Author

NabiKAZ commented Jun 9, 2017

I thinks must be use call_user_func_array function or ReflectionClass core class.

@NabiKAZ
Copy link
Contributor Author

NabiKAZ commented Jun 9, 2017

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

@noplanman
Copy link
Member

noplanman commented Jun 9, 2017

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.

@NabiKAZ
Copy link
Contributor Author

NabiKAZ commented Jun 9, 2017

wow that's perfect.
I did not know array_chunk function, this is nice function.

@NabiKAZ NabiKAZ closed this as completed Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants