Skip to content

Instantly share code, notes, and snippets.

@bigwhoop
Created February 7, 2011 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigwhoop/815294 to your computer and use it in GitHub Desktop.
Save bigwhoop/815294 to your computer and use it in GitHub Desktop.
<?php
function en(array $a)
{
$b = array($a[0]);
for ($i = 1, $l = count($a); $i < $l; $i++) {
$j = count($b) - 1;
$x = explode('-', $b[$j]);
if ((reset($x) >= end($x) && $a[$i] == end($x) - 1) || (reset($x) <= end($x) && $a[$i] == end($x) + 1)) {
$b[$j] = reset($x) . '-' . $a[$i];
} else {
$b[] = $a[$i];
}
}
return $b;
}
function de(array $a)
{
$b = array();
foreach ($a as $i) {
$x = explode('-', $i);
$b = array_merge($b, range(reset($x), end($x)));
}
return $b;
}
$numbers = array(13,81,80,79,78,79,77,76,19,40,41,42,43,44,45,48);
echo join(',', en($numbers)) . '<br />';
$numbers = array(13,'81-78',79,'77-76',19,'40-45',48);
echo join(',', de($numbers)) . '<br />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment