PHP Round Up to nearest Array

//array list
$array = array(1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 150, 250, 300, 350, 400, 450, 500, 600, 700, 800, 900, 1000);

//array round up function
function getArrayRoundUp($array, $number) {

sort($array);
foreach ($array as $a) {
if ($a >= $number) return $a;
}
return end($array);
}

//assume this is the value
$value = 904.5;

$nearest = getArrayRoundUp($array, $value);

//the expect result will be 1000
echo $nearest;

 

Leave a Reply

Your email address will not be published. Required fields are marked *