Generating an array of school grades I have a catalogue of activities that are marked based on the age group that they are relevant to. There are 14 checkboxes for checking what grades. K, 1 - 12, and Adult. I have a switch statement to change the K and Adult to a numeric representation.

The following PHP code grabs the grades checked and displays them in consecutive groups.
Aka. K, 1, 2, 3, 5, 6, 7, 10, 11, Adult will display as K - 3, 5 - 7, 10 - Adult.

What I would like to ask is, is this a clumsy solution for my problem? Can I learn to make this more efficient or less prone to issues?

// generated through database, but set manually here for testing
$new_grades_array = array(K, 1, 2, 3, 5, 6, 7, 10, 11, Adult)

// grades related variables 
$last = ""; // records previous grade in loop for checking consecutiveness
$display = ""; // concatenated display of results
$conseq = FALSE; // records if previous grade was consecutive

for ($i = 0; $i &lt; count($new_grades_array); $i++):

     // set grade digit for K and Adult
     switch ($new_grades_array[$i]):
           case "K":
               $currentgrade_dig = 0;
               break;
           case "Adult":
               $currentgrade_dig = 13;
               break;
           default:
               $currentgrade_dig = $new_grades_array[$i];
     endswitch;

     // concatenates string to display variable based on situation
     if ($i == 0): // if the first grade listed

          $display = $new_grades_array[$i];

     elseif ($i +1 == count($new_grades_array)): //if the last grade listed

          if ($conseq != FALSE): 
               $display .= " - " . $last . ", " . $new_grades_array[$i]; 
          else: 
               $display .= ", " . $new_grades_array[$i];
          endif;

         $conseq = FALSE;

     elseif ($currentgrade_dig - $last ==1): // if consecutive number from previous
          $conseq = TRUE;

     else: // if not a consecutive number

          if ($conseq != FALSE): 
               $display .= " - " . $last . ", " . $new_grades_array[$i];
               $conseq = FALSE;
          else:
               $display .= ", " . $new_grades_array[$i];
          endif;
     endif;

     $last = $new_grades_array[$i];

endfor;

print $display;
Here are some tips.

1) Have a function that returns the grade value.

This helps avoid duplicated code and increases readablilty.

Code:

function getGradeValue($val){
  switch ($val) {
  case "K":
    $val = 0;
    break;
  case "Adult":
    $val = 13;
    break;
  default:
  }
  return $val;
}


2) Create a function to retrieve the final output.

This allow for an easier way to test the overall functionality and helps to eliminate global variables.

3) Create unit tests to speed up testing

Simple Testcases:

function println( $str ){
  print( $str . "&lt;br/&gt;\n" );
}
function testThis( $arr, $expect ){
  $result = getGroupedGradesAsString( $arr );
  if( $result == $expect ){
    println( "Passed:" );
  }else{
    println( "Fail:" . $result . " != " . $expect );
  }
}
testThis( array("K", 1, 2, 3, 5, 6, 7, 10, 11, "Adult"), "K - 3, 5 - 7, 10 - 11, Adult" );
testThis( array("K"),  "K" );
testThis( array("K", "Adult"),  "K, Adult" );
testThis( array("K", 1, 2, 7, 11, 12), "K - 2, 7, 11 - 12" );
testThis( array( 6, 7, 8), "6 - 8" );
testThis( array("K", 2, 4, 6, 8, 10, 12), "K, 2, 4, 6, 8, 10, 12" );
testThis( array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, "Adult"),  "1 - Adult" );


4) Use a loop to find the last consistent number.

Refer to getGroupedGradesAsString() in the final code for more information.

Final Code:

&lt;?php
// By Larry Battle [http://bateru.com/news/]
function getGradeValue($val){
  switch ($val) {
  case "K":
    $val = 0;
    break;
  case "Adult":
    $val = 13;
    break;
  default:
  }
  return $val;
}
function getGroupedGradesAsString($arr) {
  $output = "";
  for ($i = 0, $len = count($arr); $i &lt; $len; $i++) {
    $output .= (($i) ? ", " : "" ) . $arr[$i];
    if( getGradeValue($arr[$i]) == getGradeValue($arr[$i+1])-1 ){
      do{
        $i++;
      }while( getGradeValue($arr[$i]) == getGradeValue($arr[$i+1])-1);
      $output .= " - " . $arr[$i];
    }
  }
  return $output;
}
?&gt;


Demo here: http://codepad.org/zi8oavlv

Here's a shorter version but might be a litter bit harder to understand.

&lt;?php
    // By Larry Battle [http://bateru.com/news/]
function getGradeValue($val){
    return ( $val == "K" || $val == "Adult" ) ? (($val == "K") ? 0 : 13 ) : $val;
}
function getGroupedGradesAsString($arr) {
  $output = "";
  for ($i = 0, $len = count($arr); $i &lt; $len; $i++) {
    $output .= (($i) ? ", " : "" ) . $arr[$i];
    if( getGradeValue($arr[$i]) !== getGradeValue($arr[$i+1])-1 ){
        continue;
    }
    while( getGradeValue($arr[++$i]) === getGradeValue($arr[$i+1])-1);
    $output .= " - " . $arr[$i];
  }
  return $output;
}
?&gt;