What are some efficient ways to handle data aggregation and calculation in PHP when working with arrays from a database?

When working with arrays from a database in PHP, it is important to efficiently handle data aggregation and calculations to avoid performance issues. One efficient way to handle this is by using built-in array functions such as array_map, array_reduce, and array_filter to manipulate and aggregate data. Additionally, utilizing SQL queries to perform calculations directly in the database can also improve performance.

// Example of using array functions for data aggregation and calculation
$data = [
    ['id' => 1, 'value' => 10],
    ['id' => 2, 'value' => 20],
    ['id' => 3, 'value' => 30]
];

// Calculate the sum of 'value' field using array_reduce
$sum = array_reduce($data, function($carry, $item) {
    return $carry + $item['value'];
}, 0);

echo "Sum of values: $sum";