<?php
$ar = [["count"=>6,"year"=>2019,"month"=>10,"name"=>"P","id"=>3,],
["count"=>2,"year"=>2019,"month"=>10,"name"=>"s.vimal","id"=>1,],
["count"=>4,"year"=>2019,"month"=>10,"name"=>"MR","id"=>2,],
["count"=>11,"year"=>2019,"month"=>11,"name"=>"s.vimal","id"=>1,],
["count"=>1,"year"=>2019,"month"=>12,"name"=>"MR","id"=>2,],
["count"=>4,"year"=>2019,"month"=>11,"name"=>"vimaltest","id"=>10,],
["count"=>1,"year"=>2019,"month"=>12,"name"=>"vimaltest","id"=>10,]];
$mon_ar = array_unique(array_column($ar,'month'));
$year_ar = array_unique(array_column($ar,'year'));
foreach($ar as $rec){
foreach($year_ar as $year){
foreach($mon_ar as $month){
if (!isset($result[$year.$month])) $result[$year.$month] = [ 'month' => $month, 'year' => $year];
if ($rec['year'] == $year && $rec['month'] <= $month) {
if (!isset($result[$year.$month][$rec['name']])) $result[$year.$month][$rec['name']] = 0;
$result[$year.$month][$rec['name']] += $rec['count'];
}
}
}
}
sort($result);
print_r($result);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- Array
(
[0] => Array
(
[month] => 10
[year] => 2019
[P] => 6
[s.vimal] => 2
[MR] => 4
)
[1] => Array
(
[month] => 11
[year] => 2019
[P] => 6
[s.vimal] => 13
[MR] => 4
[vimaltest] => 4
)
[2] => Array
(
[month] => 12
[year] => 2019
[P] => 6
[s.vimal] => 13
[MR] => 5
[vimaltest] => 5
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[0] => Array
(
[month] => 10
[year] => 2019
[P] => 6
[s.vimal] => 2
[MR] => 4
)
[1] => Array
(
[month] => 11
[year] => 2019
[P] => 6
[s.vimal] => 13
[MR] => 4
[vimaltest] => 4
)
[2] => Array
(
[month] => 12
[year] => 2019
[P] => 6
[s.vimal] => 13
[MR] => 5
[vimaltest] => 5
)
)
preferences:
118.49 ms | 403 KiB | 176 Q