1. <?php
  2. /**
  3. * Attaches timestamp metadata to a batch of data.
  4. *
  5. * @param array $data An array of data items (e.g., files, records).
  6. * @param string $timestampKey The key to store the timestamp metadata.
  7. * @return array The modified data array with timestamp metadata added.
  8. */
  9. function attachTimestamps(array $data, string $timestampKey = 'timestamp')
  10. {
  11. // Get the current timestamp.
  12. $timestamp = time();
  13. // Iterate over the data and add the timestamp metadata.
  14. foreach ($data as &$item) { // Use & to modify the original array
  15. $item[$timestampKey] = $timestamp; // Add the timestamp to each item
  16. }
  17. return $data;
  18. }
  19. /**
  20. * Example usage:
  21. */
  22. // Sample data (replace with your actual data).
  23. $batchData = [
  24. ['name' => 'File 1', 'content' => 'Some content'],
  25. ['name' => 'File 2', 'content' => 'More content'],
  26. ['name' => 'File 3', 'content' => 'Even more content'],
  27. ];
  28. // Attach the timestamp metadata.
  29. $timestampedData = attachTimestamps($batchData);
  30. // Output the timestamped data (for demonstration).
  31. print_r($timestampedData);
  32. ?>

Add your comment