1. <?php
  2. /**
  3. * Removes duplicate date values from an array.
  4. *
  5. * @param array $dates An array of date strings (e.g., 'YYYY-MM-DD').
  6. * @return array An array with unique dates, maintaining original order.
  7. */
  8. function removeDuplicateDates(array $dates): array {
  9. $uniqueDates = []; // Initialize an array to store unique dates.
  10. foreach ($dates as $date) {
  11. // Basic sanity check: ensure the value is a string
  12. if (!is_string($date)) {
  13. continue; // Skip non-string values
  14. }
  15. // Check if the date is already in the uniqueDates array
  16. if (!in_array($date, $uniqueDates, true)) { // strict comparison
  17. $uniqueDates[] = $date; // Add the date to the uniqueDates array
  18. }
  19. }
  20. return $uniqueDates;
  21. }
  22. //Example usage
  23. /*
  24. $dateArray = ['2023-10-26', '2023-10-27', '2023-10-26', '2023-10-28', '2023-10-27'];
  25. $uniqueArray = removeDuplicateDates($dateArray);
  26. print_r($uniqueArray); // Output: Array ( [0] => 2023-10-26 [1] => 2023-10-27 [2] => 2023-10-28 )
  27. */
  28. ?>

Add your comment