- array_chunk: documentation ( source)
- array_shift: documentation ( source)
- range: documentation ( source)
- array_pop: documentation ( source)
- printf: documentation ( source)
<?php
class Task {
function __construct($id, $start, $end) {
$this->id = $id;
$this->start = $start;
$this->end = $end;
$this->pos = $this->start;
}
function execute() {
if ($this->pos < $this->end) {
return $this->pos++;
} else return false;
}
}
$range = range(1, 100);
$ranges = array_chunk($range, 10);
$tasks = array();
while (count($ranges)) {
$range = array_shift($ranges);
$tasks[] = new Task(
count($tasks) + 1,
array_shift($range),
array_pop($range));
}
while (count($tasks)) {
foreach ($tasks as $id => $task) {
if ($task->execute() === false) {
printf("task %d complete\n", $task->id);
unset($tasks[$id]);
} else printf("task %d position %d\n", $task->id, $task->pos);
}
}
?>