-1

I Developed one program but no to set any logic here.

Ex. I input 1,2,3,7,9 then the result is display like 1-3,7,9. Someone Master Please help

  • where is the program you developed? – uno Dec 04 '15 at 06:04
  • Please elaborate more about what you are looking for with this ? It's very unclear to say with current question. – Prateek Dec 04 '15 at 06:05
  • no any logic is set here to display result.can you help me. For Example. In Text Box i Input 1,2,3,4,6,8,9 Display Result : 1-4,6,8-9 Look Like This. Here No break of Number 1,2,3,4 so it display 1-4 then break series so display 6 and same as 8-9. So which Logic is use. – user3156153 Dec 04 '15 at 06:05
  • Please post your codes – Raptor Dec 04 '15 at 07:55

2 Answers2

0

After you take inputs to integer array,

<?php

$inputs = array(1, 2, 3, 5, 6, 7, 8, 10, 55, 56, 100);
$length = sizeof($inputs);

if($length > 1) {

    $current = 0;
    $next = 0;
    $group_start = $inputs[0];
    $group_end = 0;
    $output = array();

    for($i = 0; $i < $length - 1; $i++) {
        $current = $inputs[$i];
        $next = $inputs[$i + 1];
        if($current != $next - 1) { // if there is a break
            $group_end = $current;
            if($group_start == $group_end) {
                array_push($output, $group_start);
            }
            else {
                array_push($output, $group_start . " - " . $group_end);
            }
            $group_start = $next;
        }
    }

    //check for last element in inputs array
    if($group_start == $next) { // if there was a break
        array_push($output, $group_start);
    }
    else {
        array_push($output, $group_start . " - " . $next);
    }
    echo implode(", ", $output);
}
snvrthn
  • 385
  • 1
  • 10
  • Thanks snvrthn. But Here Some Problem in Output. This above code out put is 1 - 5, 6 - 10, 55 but it's orignal output is look like 1-8, 10, 55. Can you Please help in solve mistake. – user3156153 Dec 04 '15 at 09:05
  • Thanks snvrthn. But Here Some Problem in Output. This above code out put is 1 - 5, 6 - 10, 55 but it's orignal output is look like 1-8, 10, 55. Can you Please help in solve mistake. – user3156153 Dec 05 '15 at 06:18
  • I updated the answer please check if it is ok. – snvrthn Dec 08 '15 at 04:25
0

Something about this will work for you..

$input = array(1,3,2,5,7,8);
sort($input);

$output = array();

foreach($input as $tmp){
    //initialize for first run
    if(!isset($start)) 
    {
        //get the first character and ignore the rest of the execution for the first number
        $start = $tmp;
        $prev = $tmp;
        continue;
    }
    //if the numbers are in series, get the number as previous
    if( $prev+1 == $tmp )
    {
        $prev = $tmp;
    }
    //else get the number, and reset our series
    else
    {
        $output[] = array($start,$prev);
        $start = $tmp;
        $prev = $tmp;
    }
}

    //one more time to battle last input not being properly processed
    $output[] = array($start,$prev);

print_r($output);
Elentriel
  • 1,237
  • 8
  • 21