Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

php - Implode sorted number array to string by commas and merge intervals

I would like to implode an array, but with one difference. I would like to merge intervals with a - sign. How can this be done? (The array is ordered!)

Examples:

array(1,2,3,6,8,9) => "1-3,6,8-9"
array(2,4,5,6,8,10) => "2,4-6,8,10"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work for you:

First for every iteration we simply append the current number of the iteration to the $result string:

$result .= $arr[$i];

After this we check in a while loop if there exists a next element in the array(1) and it follows up the number from the current iteration(2). We do that until the condition evaluates as false:

//(1)Check if next element exists     (2)Check if next element follows up the prev one
      ┌───────┴───────┐    ┌───────────┴────────────┐      
while(isset($arr[$i+1]) && $arr[$i] + 1 == $arr[$i+1] && ++$range)
    $i++;

Then we check if we have a range (e.g. 1-3) or not. If yes then we append the dash and the end number of the range to the result string:

if($range)
    $result .= "-" . $arr[$i];

At the end we also check if we are at the end of the array and don't need to append a comma anymore:

if($i+1 < $l)
    $result .= ",";

Code:

<?php

    $arr = array(1,2,3,6,8,9);
    $result = "";
    $range = 0;

    for($i = 0, $l = count($arr); $i < $l; $i++){

        $result .= $arr[$i];

        while(isset($arr[$i+1]) && $arr[$i] + 1 == $arr[$i+1] && ++$range)
            $i++;

        if($range)
            $result .= "-" . $arr[$i];

        if($i+1 < $l)
            $result .= ",";

        $range = 0;   

    }

    echo $result;

?>

output:

1-3,6,8-9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...