1. <?php
  2. /**
  3. * Strips metadata from user records in a legacy project.
  4. *
  5. * This function iterates through a collection of user records
  6. * and removes specified metadata fields. It's designed to be
  7. * compatible with older PHP versions.
  8. *
  9. * @param array $users An array of user records. Each record is an associative array.
  10. * @param array $metadata_to_remove An array of metadata field names to remove.
  11. * @return array The modified array of user records with metadata stripped.
  12. */
  13. function stripUserMetadata(array $users, array $metadata_to_remove): array
  14. {
  15. if (empty($users) || empty($metadata_to_remove)) {
  16. return $users; // Nothing to do.
  17. }
  18. foreach ($users as $user) {
  19. foreach ($metadata_to_remove as $field) {
  20. if (array_key_exists($field, $user)) {
  21. unset($user[$field]); // Remove the metadata field.
  22. }
  23. }
  24. }
  25. return $users;
  26. }
  27. // Example usage (replace with your actual user data and metadata)
  28. $users = [
  29. [
  30. 'id' => 1,
  31. 'username' => 'john_doe',
  32. 'email' => 'john.doe@example.com',
  33. 'created_at' => '2023-10-26',
  34. 'profile_picture' => 'profile.jpg',
  35. 'location' => 'New York',
  36. 'metadata_field_1' => 'some_value',
  37. 'metadata_field_2' => 'another_value'
  38. ],
  39. [
  40. 'id' => 2,
  41. 'username' => 'jane_doe',
  42. 'email' => 'jane.doe@example.com',
  43. 'created_at' => '2023-10-27',
  44. 'profile_picture' => 'profile.png',
  45. 'location' => 'London',
  46. 'metadata_field_1' => 'different_value',
  47. 'metadata_field_2' => 'yet_another_value'
  48. ]
  49. ];
  50. $metadata_to_remove = ['metadata_field_1', 'metadata_field_2'];
  51. $stripped_users = stripUserMetadata($users, $metadata_to_remove);
  52. // Output the stripped user data (for demonstration)
  53. print_r($stripped_users);
  54. ?>

Add your comment