Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 24, 2012 23:39
Show Gist options
  • Save stefanocudini/2189134 to your computer and use it in GitHub Desktop.
Save stefanocudini/2189134 to your computer and use it in GitHub Desktop.
return array value near to a value
<?
//restituisce il valore dell'array piu vicino al valore richiesto!!!!
$value = intval($argv[1]);
$tnsizes = array(10,50,100,150,300);
echo nearValue($value, $tnsizes);
function nearValue($tnsize, $tnsizes)
{
if(in_array($tnsize,$tnsizes))
return $tnsize;
else
{
foreach($tnsizes as $s)
$rr[]= abs($tnsize - $s - 1);
//-1 serve nei casi in cui es. si chiede 250 e si deve sciegliere fra 200 o 300;
asort($rr);
return $tnsizes[key($rr)];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment