Reputation: 121
I have an array:
$array = [1, 2, 3, 4, 6, 7, 8, 10, 12, 14, 15, 16, 20, 21, 26, 27, 28];
and I want to convert it to a string where sequences are seperated by a dash. So the above array would become: 1-4, 6-8, 10, 12, 14-16, 20, 21, 26-28
I have tried looping through an error and checking of the previous number + 1 is equal to the current number to determine if I'm in a loop. Didn't quite manage it and it felt very inefficient. Any tips and/or ideas on how to fix this?
This code is unfinished and not working completely:
asort($zips);
$formatted = '';
$sequence = [];
$inSequenceLoop = false;
foreach ($zips as $key => $zip) {
// $prev = $key === 0 ? false : $zips[$key - 1];
$current = $zip;
$next = $key === count($zips) - 1 ? false : $zips[$key + 1];
if ($current + 1 === $next) {// || ($next === false && ($prev + 1) === $current)) {
$sequence[] = $current;
$inSequenceLoop = true;
} else {
if ($inSequenceLoop) {
// end of sequence
if (count($sequence) === 1) {
$formatted .= $sequence[0] . ($next ? ',' : '');
} else {
$formatted .= array_shift($sequence) . '-' . array_pop($sequence) . ($next ? ',' : '');
}
// create empty sequence
$inSequenceLoop = false;
$sequence = [];
} else {
// loner
$formatted .= $current . ($next ? ',' : '');
}
}
}
Upvotes: 2
Views: 375
Reputation: 41810
One approach would be to first split the array into sets:
$pre = $key = 0; // initialize previous value and set key
foreach ($array as $number) {
if ($number > $pre + 1) $key++; // if number is not next in sequence, increment key
$sets[$key][] = $pre = $number; // append to current key and set previous to current
}
then map a formatting function to return the range strings, and implode the formatted ranges.
$result = implode(', ', array_map(function($set) {
// return either single element or dash separated first and last element
return count($set) == 1 ? reset($set) : reset($set) . '-' . end($set);
}, $sets));
If a range of two elements should be formatted as individual elements, the format function could take that into account as well.
$result = implode(', ', array_map(function($set) {
switch (count($set)) {
case 1: return reset($set);
case 2: return reset($set) . ', ' . $set[1];
default: return reset($set) . '-' . end($set);
}
}, $sets));
Upvotes: 1