<?php
/**
* Function to replace values in an API response for data migration.
*
* @param array $response The API response data (as an associative array).
* @param array $replacements An associative array of key => new_value pairs.
* @return array The modified API response.
*/
function migrateData(array $response, array $replacements): array
{
// Iterate through the replacements array.
foreach ($replacements as $key => $new_value) {
// Check if the key exists in the response.
if (array_key_exists($key, $response)) {
// Replace the value if it exists.
$response[$key] = $new_value;
}
}
return $response;
}
/**
* Example usage.
*/
// Sample API response.
$apiResponse = [
'id' => 123,
'name' => 'Old Name',
'email' => 'old.email@example.com',
'status' => 'inactive',
'created_at' => '2023-10-26',
];
// Define the replacements.
$replacements = [
'name' => 'New Name',
'email' => 'new.email@example.com',
'status' => 'active'
];
// Migrate the data.
$migratedResponse = migrateData($apiResponse, $replacements);
// Output the migrated response.
print_r($migratedResponse);
?>
Add your comment