1. <?php
  2. /**
  3. * Teardown function for debugging date values with default values.
  4. *
  5. * @param array $data An array of data containing dates.
  6. * @param array $defaults An array of default date values.
  7. * @return array The modified data with default dates.
  8. */
  9. function teardownDates(array $data, array $defaults): array
  10. {
  11. $newData = [];
  12. foreach ($data as $key => $value) {
  13. if (is_string($value) || is_int($value) || is_float($value)) { // Check if it's a potential date
  14. if (isset($defaults[$key])) {
  15. $newData[$key] = $defaults[$key]; // Use default if available
  16. } else {
  17. $newData[$key] = null; // Use null if no default is found
  18. }
  19. } else {
  20. $newData[$key] = $value; // Keep non-date values as is
  21. }
  22. }
  23. return $newData;
  24. }
  25. // Example usage (can be removed for just the function)
  26. /*
  27. $data = [
  28. 'date1' => '2023-10-26',
  29. 'date2' => 1698417600,
  30. 'date3' => 'invalid date',
  31. 'other' => 'some value'
  32. ];
  33. $defaults = [
  34. 'date1' => '2023-01-01',
  35. 'date2' => 0,
  36. 'date4' => '2024-01-01'
  37. ];
  38. $teardownData = teardownDates($data, $defaults);
  39. print_r($teardownData);
  40. */
  41. ?>

Add your comment