Loops
Installation
You can install the package via composer:
composer require isaeken/loops
Usage
Basic Usage
loop(5, function ($loop, $instance) {
return $loop->odd;
}); // [false, true, false, true, false]
Get current loop
loop(2 ,function ($loop, $instance) {
return [
'iteration' => $loop->iteration,
'index' => $loop->index,
'remaining' => $loop->remaining,
'count' => $loop->count,
'first' => $loop->first,
'last' => $loop->last,
'odd' => $loop->odd,
'even' => $loop->even,
];
});
// [
// [
// 'iteration' => 0,
// 'index' => 0,
// 'remaining' => 1,
// 'count' => 2,
// 'first' => true,
// 'last' => false,
// 'odd' => false,
// 'even' => true,
// ],
// [
// 'iteration' => 1,
// 'index' => 1,
// 'remaining' => 0,
// 'count' => 2,
// 'first' => false,
// 'last' => true,
// 'odd' => true,
// 'even' => false,
// ]
// ]
Break the loop
loop(3, function ($loop, $instance) {
if ($loop->index > 1) {
$instance->stop();
}
return $loop->index;
}); // [0, 1]
Loop random times
loop_random(function ($loop, $instance) {
return $loop->index;
}); // executed random times.
$min = 5;
$max = 10;
loop_random(function ($loop, $instance) {
return $loop->index;
}, $min, $max);
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.