<?php
/**
* Removes duplicate date values from an array.
*
* @param array $dates An array of date strings (e.g., 'YYYY-MM-DD').
* @return array An array with unique dates, maintaining original order.
*/
function removeDuplicateDates(array $dates): array {
$uniqueDates = []; // Initialize an array to store unique dates.
foreach ($dates as $date) {
// Basic sanity check: ensure the value is a string
if (!is_string($date)) {
continue; // Skip non-string values
}
// Check if the date is already in the uniqueDates array
if (!in_array($date, $uniqueDates, true)) { // strict comparison
$uniqueDates[] = $date; // Add the date to the uniqueDates array
}
}
return $uniqueDates;
}
//Example usage
/*
$dateArray = ['2023-10-26', '2023-10-27', '2023-10-26', '2023-10-28', '2023-10-27'];
$uniqueArray = removeDuplicateDates($dateArray);
print_r($uniqueArray); // Output: Array ( [0] => 2023-10-26 [1] => 2023-10-27 [2] => 2023-10-28 )
*/
?>
Add your comment