What is the purpose of grouping data in PHP when working with tabular statistics?

Grouping data in PHP when working with tabular statistics allows for easier analysis and summarization of the data. By grouping data based on certain criteria, such as categories or time periods, you can quickly calculate totals, averages, or other statistics for each group. This can help in identifying patterns, trends, or outliers in the data.

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

$groupedData = [];

foreach ($data as $row) {
    $category = $row['category'];
    if (!isset($groupedData[$category])) {
        $groupedData[$category] = ['count' => 0, 'total' => 0];
    }
    $groupedData[$category]['count']++;
    $groupedData[$category]['total'] += $row['value'];
}

print_r($groupedData);