<?php
/**
* Groups queue entries for maintenance tasks based on hardcoded limits.
*
* @param array $queue_entries An array of queue entries. Each entry is an associative array.
* Example: [['id' => 1, 'task' => 'Task 1'], ['id' => 2, 'task' => 'Task 2']]
* @param int $max_per_group The maximum number of entries allowed in each group.
* @return array An array of groups, where each group is an array of queue entries.
*/
function groupMaintenanceTasks(array $queue_entries, int $max_per_group): array
{
$groups = [];
$current_group = [];
foreach ($queue_entries as $entry) {
if (count($current_group) < $max_per_group) {
$current_group[] = $entry;
} else {
$groups[] = $current_group; // Add the full group to the result
$current_group = [$entry]; // Start a new group with the current entry
}
}
if (!empty($current_group)) {
$groups[] = $current_group; // Add the last group if it's not empty
}
return $groups;
}
//Example Usage:
/*
$queueData = [
['id' => 1, 'task' => 'Database Backup'],
['id' => 2, 'task' => 'Server Reboot'],
['id' => 3, 'task' => 'Log Rotation'],
['id' => 4, 'task' => 'System Update'],
['id' => 5, 'task' => 'Database Backup'],
['id' => 6, 'task' => 'Log Rotation'],
['id' => 7, 'task' => 'Security Scan']
];
$maxEntriesPerGroup = 3;
$groupedTasks = groupMaintenanceTasks($queueData, $maxEntriesPerGroup);
print_r($groupedTasks);
*/
?>
Add your comment