$fromDb = [
['id'=>1, 'day'=>2, 'month'=>1],
['id'=>2, 'day'=>22, 'month'=>3],
['id'=>2, 'day'=>31, 'month'=>2],
['id'=>4, 'day'=>12, 'month'=>2],
['id'=>5, 'day'=>4, 'month'=>3]
];
function groupBy(array $elements, callable $getUniqueKey) {
$grouped = [];
foreach ($elements as $element) {
$uniqueKey = $getUniqueKey($element);
$grouped[$uniqueKey][] = $element;
}
return $grouped;
}
$groupedByMonth = groupBy($fromDb, function($row) {
return $row['month'];
});