Created
June 29, 2015 09:01
-
-
Save kdes70/78b2239164112e035758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Подстовляем дефис в интервал десятичных чисел | |
* *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