I have this array with year as its key and count as its value,
$dateRange = array([
"1990" => 10,
"1991" => 12,
"1992" => 12,
"1993" => 12,
"1994" => 12,
"1995" => 12,
"1996" => 6
]);
and how can I make it look like this *(getting consecutive year/s with a count/value of 12 then count its total value)
$dateRange = ([
"1990" => 10,
"1991-1995" => 60,
"1996" => 6
]);
My Answer:
"2019" => 5,
"2020" => 8,
"2021" => 5,
"2022" => 3,
"2023" => 2,
"2024" => 8
);
To group the values by count, you can use the following PHP code:
php
$groupedArray = [];
foreach ($dateRange as $year => $count) {
if (!isset($groupedArray[$count])) {
$groupedArray[$count] = [];
}
$groupedArray[$count][] = $year;
}
print_r($groupedArray);
This code will output an array where the keys are the counts and the values are arrays of years with that count.
Rate this post
4 of 5 based on 8284 votesComments