<?php
/**
* Mirrors data of entries with basic sanity checks for manual execution.
*
* @param array $sourceData Array of source data entries.
* @param array $destinationData Array of destination data entries.
* @param array $checkFields Array of fields to perform sanity checks on.
* @return array Array of updated destination data entries.
*/
function mirrorData(array $sourceData, array $destinationData, array $checkFields): array
{
$updatedData = [];
foreach ($sourceData as $sourceEntry) {
$matchingEntry = null;
foreach ($destinationData as $destEntry) {
// Match based on a common identifier (e.g., ID)
if (isset($sourceEntry['id']) && isset($destEntry['id']) && $sourceEntry['id'] == $destEntry['id']) {
$matchingEntry = $destEntry;
break;
}
}
if ($matchingEntry !== null) {
// Mirror the data, performing sanity checks
$updatedEntry = $matchingEntry;
foreach ($checkFields as $field) {
if (isset($sourceEntry[$field]) && isset($matchingEntry[$field]) && $sourceEntry[$field] !== $matchingEntry[$field]) {
// Sanity check: Data mismatch
error_log("Sanity check failed for field: " . $field . " - Source: " . $sourceEntry[$field] . ", Destination: " . $matchingEntry[$field]);
// Optionally, handle the mismatch (e.g., log, skip, alert)
// For now, we'll just leave the data as is.
}
}
$updatedData[] = $updatedEntry;
} else {
// No matching entry found in destination data
error_log("No matching entry found in destination data for ID: " . (isset($sourceEntry['id']) ? $sourceEntry['id'] : 'N/A'));
$updatedData[] = $sourceEntry; // Keep the source entry
}
}
return $updatedData;
}
// Example Usage (for manual execution)
/*
$sourceData = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com', 'age' => 30],
['id' => 2, 'name' => 'Bob', 'email' => 'bob@example.com', 'age' => 25],
['id' => 3, 'name' => 'Charlie', 'email' => 'charlie@example.com', 'age' => 40],
];
$destinationData = [
['id' => 1, 'name' => 'Alice', 'email' => 'alice@example.com', 'age' => 30],
['id' => 2, 'name' => 'Robert', 'email' => 'robert@example.com', 'age' => 26], // Different name and age
];
$checkFields = ['name', 'email', 'age'];
$updatedData = mirrorData($sourceData, $destinationData, $checkFields);
// Output the updated data (for manual inspection)
print_r($updatedData);
*/
?>
Add your comment