1. <?php
  2. /**
  3. * Groups queue entries for maintenance tasks based on hardcoded limits.
  4. *
  5. * @param array $queue_entries An array of queue entries. Each entry is an associative array.
  6. * Example: [['id' => 1, 'task' => 'Task 1'], ['id' => 2, 'task' => 'Task 2']]
  7. * @param int $max_per_group The maximum number of entries allowed in each group.
  8. * @return array An array of groups, where each group is an array of queue entries.
  9. */
  10. function groupMaintenanceTasks(array $queue_entries, int $max_per_group): array
  11. {
  12. $groups = [];
  13. $current_group = [];
  14. foreach ($queue_entries as $entry) {
  15. if (count($current_group) < $max_per_group) {
  16. $current_group[] = $entry;
  17. } else {
  18. $groups[] = $current_group; // Add the full group to the result
  19. $current_group = [$entry]; // Start a new group with the current entry
  20. }
  21. }
  22. if (!empty($current_group)) {
  23. $groups[] = $current_group; // Add the last group if it's not empty
  24. }
  25. return $groups;
  26. }
  27. //Example Usage:
  28. /*
  29. $queueData = [
  30. ['id' => 1, 'task' => 'Database Backup'],
  31. ['id' => 2, 'task' => 'Server Reboot'],
  32. ['id' => 3, 'task' => 'Log Rotation'],
  33. ['id' => 4, 'task' => 'System Update'],
  34. ['id' => 5, 'task' => 'Database Backup'],
  35. ['id' => 6, 'task' => 'Log Rotation'],
  36. ['id' => 7, 'task' => 'Security Scan']
  37. ];
  38. $maxEntriesPerGroup = 3;
  39. $groupedTasks = groupMaintenanceTasks($queueData, $maxEntriesPerGroup);
  40. print_r($groupedTasks);
  41. */
  42. ?>

Add your comment