<?php
/**
* Strips metadata from user records in a legacy project.
*
* This function iterates through a collection of user records
* and removes specified metadata fields. It's designed to be
* compatible with older PHP versions.
*
* @param array $users An array of user records. Each record is an associative array.
* @param array $metadata_to_remove An array of metadata field names to remove.
* @return array The modified array of user records with metadata stripped.
*/
function stripUserMetadata(array $users, array $metadata_to_remove): array
{
if (empty($users) || empty($metadata_to_remove)) {
return $users; // Nothing to do.
}
foreach ($users as $user) {
foreach ($metadata_to_remove as $field) {
if (array_key_exists($field, $user)) {
unset($user[$field]); // Remove the metadata field.
}
}
}
return $users;
}
// Example usage (replace with your actual user data and metadata)
$users = [
[
'id' => 1,
'username' => 'john_doe',
'email' => 'john.doe@example.com',
'created_at' => '2023-10-26',
'profile_picture' => 'profile.jpg',
'location' => 'New York',
'metadata_field_1' => 'some_value',
'metadata_field_2' => 'another_value'
],
[
'id' => 2,
'username' => 'jane_doe',
'email' => 'jane.doe@example.com',
'created_at' => '2023-10-27',
'profile_picture' => 'profile.png',
'location' => 'London',
'metadata_field_1' => 'different_value',
'metadata_field_2' => 'yet_another_value'
]
];
$metadata_to_remove = ['metadata_field_1', 'metadata_field_2'];
$stripped_users = stripUserMetadata($users, $metadata_to_remove);
// Output the stripped user data (for demonstration)
print_r($stripped_users);
?>
Add your comment