Skip to content

Instantly share code, notes, and snippets.

@kdes70
Created June 29, 2015 09:01
Show Gist options
  • Save kdes70/78b2239164112e035758 to your computer and use it in GitHub Desktop.
Save kdes70/78b2239164112e035758 to your computer and use it in GitHub Desktop.
/**
* Подстовляем дефис в интервал десятичных чисел
* *1, 1.2, 1.3, 1.4, 2, 3 => result 1-1.4, 2, 3*
*
* @param $array
* @param bool $str если TRUE возврощать будет страку
*
* @return array|string
*/
public static function spacing($array, $str = FALSE)
{
if (is_string($array))
{
$array = explode(',', $array);
}
$result = [];
for($i = 0, $size = count($array); $i < $size; $i++)
{
$start = $array[$i];
$end = $start;
while(isset($array[$i + 1]) && ! bccomp($array[$i + 1] - $array[$i], 0.1, 1))
{
$end = $array[++$i];
}
$result[] = $start == $end
? $start
: $start.'-'.$end;
}
if ($str = TRUE)
{
return implode(', ', $result);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment