title: Reverse range-like functionality in php arrays
body: <p><br>
  I have an array like this:</p>
  
  <pre><code>array(0, 2, 4, 5, 6, 7, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99);
  </code></pre>
  
  <p>and I want to get it as a the following string:</p>
  
  <p><code>0, 2, 4-7, 90+</code></p>
  
  <p>Any examples out there before I start to pull hairs from my head ?<br>
  Thanks.</p>
  
  <p><strong>UPDATE:</strong><br>
  Here is the final solution I used after taking @Andy's code, and modifying it a bit.</p>
  
  <pre><code>function rangeArrayToString($rangeArray, $max = 99) {
      sort($rangeArray);
      $first = $last = null;
      $output = array();
  
      foreach ($rangeArray as $item) {
          if ($first === null) {
              $first = $last = $item;
          } else if ($last &lt; $item - 1) {
              $output[] = $first == $last ? $first : $first . '-' . $last;
              $first = $last = $item;
          } else {
              $last = $item;
          }
      }
  
      $finalAddition = $first;
  
      if ($first != $last) {
          if ($last == $max) {
              $finalAddition .= '+';
          } else {
              $finalAddition .= '-' . $last;
          }
      }
  
      $output[] = $finalAddition;
  
      $output = implode(', ', $output);
      return $output;
  }
  </code></pre>