I have a function that returns arrays of month intervals like this:
array("feb","mar","apr","oct","nov");
and it needs to be changed into this:
array("feb-apr","oct-nov");
How do I achieve this?
Below I have some example code that I've tried, but it throws an 'undefined offset: 13' notice.
$months = array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
$feature_values = array("feb","mar","apr","oct","nov");
$result = $feature_values[0];
$start_index = array_search($result, $months);
$value_count = count($feature_values);
for ($i = 0; $i < $value_count; $i++) {
if ($months[$start_index + $i] !== $feature_values[$i]) {
$result .= ' - ' . $feature_values[$i - 1];
if ($i < $value_count - 1) {
$result .= ', ' . $feature_values[$i];
$start_index = array_search($feature_values[$i], $months);
}
}
}
return $result;