Created
March 24, 2012 23:39
-
-
Save stefanocudini/2189134 to your computer and use it in GitHub Desktop.
return array value near to a value
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
//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