Sobes.tech
Junior — Middle

How is the selection and processing of aggregated data carried out?

sobes.tech AI

Answer from AI

Selection and processing of aggregated data in PHP is usually performed using functions for working with arrays and databases. First, data is collected (for example, from a database using SQL queries with aggregate functions such as SUM, COUNT, AVG), then filtered and processed in PHP. For example, you can use the array_filter function for selection and array_map for data transformation.

Example:

$data = [
    ['category' => 'A', 'value' => 10],
    ['category' => 'B', 'value' => 20],
    ['category' => 'A', 'value' => 15],
];

// Select elements of category 'A'
$filtered = array_filter($data, fn($item) => $item['category'] === 'A');

// Aggregation: sum of values in category 'A'
$sum = array_reduce($filtered, fn($carry, $item) => $carry + $item['value'], 0);

echo "Sum for category A: $sum"; // Will output 25