Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

devkon98's avatar

How to find sum of specific key tags in array

Hello i have this API code that gets values from database, this is the code:

public function toArray($request) { $currencies = explode('-',$this->currency); $dates [] = $this->date;

    foreach ($dates as $date) {
        foreach ($currencies as $currency) {
            $coin[] = unserialize($currency);
        }
    }`
    
    return [
        'currencies' => $coin,
        'date' => $date,
    ];
}

And this is the result i get from postman:

[ [ { "currencies": [ { "ALL": 100 }, { "ALL": 200 }, { "ALL": 89 } ], "date": "14-Feb-2023" }, { "currencies": [ { "USD": 100 } ], "date": "18-Feb-2023" }, { "currencies": [ { "ALL":500 "ALL": 300, "USD": 250 } ], "date": "21-Feb-2023" }, { "currencies": [ { "ALL": 30 }, { "ALL": 100, "USD": 200 } ], "date": "22-Feb-2023" }, { "currencies": [ { "USD": 200 } ], "date": "23-Feb-2023" } ] ]

I want to calculate the values for each date with specific tag example

[ [ { "currencies": [ { "ALL": 389 } ], "date": "14-Feb-2023" }, { "ALL": 800, "USD": 250 } ], "date": "21-Feb-2023" }

0 likes
3 replies
hamzaaslam's avatar
Level 1

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

1 like

Please or to participate in this conversation.