php - How to find and replace intervals in array? -
php - How to find and replace intervals in array? -
i have function returns arrays of month intervals this:
array("feb","mar","apr","oct","nov");
and needs changed this:
array("feb-apr","oct-nov");
how accomplish this?
below have illustration code i've tried, throws '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); ($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); } } } homecoming $result;
php arrays replace intervals
Comments
Post a Comment