<?php
/**
* Merges datasets of queues for staging environments with error handling.
*
* @param array $stagingQueues An array of staging queue datasets. Each element should be an associative array with a 'data' key containing the queue data.
* @param string $outputFile The path to the output file where the merged data will be written.
* @return bool True on success, false on failure.
*/
function mergeStagingQueues(array $stagingQueues, string $outputFile): bool
{
if (empty($stagingQueues)) {
error_log("Error: No staging queues provided.");
return false;
}
$mergedData = [];
foreach ($stagingQueues as $queue) {
if (!isset($queue['data']) || !is_array($queue['data'])) {
error_log("Error: Invalid queue data format. Skipping.");
continue; // Skip to the next queue
}
$mergedData = array_merge($mergedData, $queue['data']);
}
if (empty($mergedData)) {
error_log("Error: No data found in staging queues.");
return false;
}
try {
file_put_contents($outputFile, json_encode($mergedData));
return true;
} catch (Exception $e) {
error_log("Error writing to file: " . $e->getMessage());
return false;
}
}
// Example Usage (replace with your actual data and file path)
/*
$stagingQueues = [
[
'data' => [
['id' => 1, 'name' => 'Queue A'],
['id' => 2, 'name' => 'Queue B'],
],
],
[
'data' => [
['id' => 3, 'name' => 'Queue C'],
['id' => 4, 'name' => 'Queue D'],
],
],
];
$outputFile = 'merged_queues.json';
if (mergeStagingQueues($stagingQueues, $outputFile)) {
echo "Staging queues merged successfully to " . $outputFile . "\n";
} else {
echo "Staging queues merge failed.\n";
}
*/
?>
Add your comment