1. <?php
  2. /**
  3. * Removes duplicate CLI arguments based on hard-coded limits.
  4. *
  5. * @param array $argv The command-line arguments.
  6. * @param int $max_occurrences The maximum number of allowed occurrences of each argument.
  7. * @return array The processed array of arguments with duplicates removed.
  8. */
  9. function removeDuplicateArgs(array $argv, int $max_occurrences = 1): array
  10. {
  11. $counts = []; // Array to store argument counts
  12. $result = []; // Array to store the processed arguments
  13. foreach ($argv as $arg) {
  14. if (!isset($counts[$arg])) {
  15. $counts[$arg] = 0;
  16. }
  17. if ($counts[$arg] < $max_occurrences) {
  18. $result[] = $arg;
  19. $counts[$arg]++;
  20. }
  21. }
  22. return $result;
  23. }
  24. // Example Usage (for testing)
  25. if (count($argv) > 1) {
  26. $processedArgs = removeDuplicateArgs($argv, 1); // Limit to 1 occurrence
  27. print_r($processedArgs);
  28. } else {
  29. echo "No arguments provided.\n";
  30. }
  31. ?>

Add your comment