Je cherche à partir d'un tableau regroupant des années, à obtenir une string qui, si les années se suivent séparent les dites années par un tiret, sinon par une simple virgule.
Pour le tableau suivant le résultat attendu serait
$str = 2000,2002,2004-2005
$podYears = array(2000,2002,2004,2005);
$length = count($podYears);
$strYears ="";
$indice = 0;
function getStrYears($t_year,$length,$indice,$strYears)
{
if ($indice >= $length-1)
return $strYears.= $t_year[$indice];
else
{
if($t_year[$indice]+1 == $t_year[$indice+1])
$strYears.= $t_year[$indice]."-";
else
$strYears.= $t_year[$indice].",";
getStrYears($t_year,$length,$indice+1,$strYears);
}
}
$res = getStrYears($podYears,$length,$indice,$strYears);
echo $res;