Wonderful! Thank you. The while statement seems to be the piece I've been missing...as simple a concept as that may seem.
Let me ask you this -- is there any reason the $strRange = ''; needs to be there, or is that similar to C, where you protect it from grabbing whatever memory space it happens be have?
Brad
p. reagan wrote:
a little rough, but try this
$arrNumbers = array(1,3,5,6,7,8,12,13,14,15,16,21);
$strRange = '';
for ($iIndex = 0; $iIndex < sizeof($arrNumbers); $iIndex++)
{
if ($arrNumbers[$iIndex] +1 == $arrNumbers[$iIndex + 1])
{
$strRange.= $arrNumbers[$iIndex].' - ';
while ($arrNumbers[$iIndex] +1 == $arrNumbers[$iIndex + 1])
{
$iIndex++;
}
$strRange.= $arrNumbers[$iIndex].', ';
}
else
{
$strRange.= $arrNumbers[$iIndex].', ';
}
}
print preg_replace('/,\s*$/i', '', $strRange);
you'll need to get rid of any duplicate numbers to make this function the way you'd want.
p.