To calculate the values for each date with a specific tag, you can modify the toArray function in your API code as follows:
public function toArray($request)
{
// Initialize an empty array to hold the results
$results = [];
// Loop through each item in the collection
foreach ($this->collection as $item) {
// Initialize an empty array to hold the currency totals for this date
$currencyTotals = [];
// Loop through each currency for this date
foreach (unserialize($item->currency) as $currency) {
// Loop through each key/value pair in the currency array
foreach ($currency as $key => $value) {
// If this is the first time we've seen this currency, initialize the total to 0
if (!isset($currencyTotals[$key])) {
$currencyTotals[$key] = 0;
}
// Add the value to the total for this currency
$currencyTotals[$key] += $value;
}
}
// Add the date and currency totals to the results array
$results[] = [
'date' => $item->date,
'currencies' => $currencyTotals,
];
}
// Return the results
return $results;
}
This method uses two nested loops to iterate through the collection of items and the currencies for each date, respectively. It initializes an empty array to hold the results and a nested array to hold the currency totals for each date. It then iterates through the currencies for each date and for each currency, it iterates through the key/value pairs and adds the value to the total for that currency. Finally, it adds the date and currency totals to the results array and returns it