$widgetsRequested = 2467; // Our magic variable - user input
echo 'Original Number:';
echo $widgetsRequested;
echo '<br>';
$widgetBatches = array("250", "500", "1000", "2000");
rsort($widgetBatches);
foreach ($widgetBatches as $key => $val) {
} // Reverse sort values
$processWidgetTotal = $widgetBatches; // Pass it on to process
function compareWidgets1($processWidgetTotal, $number) { // run this once to compare
sort($processWidgetTotal);
foreach ($processWidgetTotal as $a) {
if ($a >= $number) return $a;
}
return end($processWidgetTotal);
}
echo 'Our Closest Matching Batch is: ';
echo $totalRemaining1 = compareWidgets1($processWidgetTotal, $widgetsRequested); // Go through and find the closest match within the widgetBatches array
echo '<br>';
echo 'Left To Allocate: ';
echo $newWidgetTotal1 = $widgetsRequested - $totalRemaining1; // Go through and find the closest match within the widgetBatches array
echo '<br>';
What I need to do is take the user input ($widgetsRequested) check it against the compareWidgets1 function (which checks the $widgetBatches array for the closest match) then echo that closest match, deduct it from the user input and loop again until the $widgetsRequested reaches 0 or the first negative result (i.e. -5).
I'm thinking i'll need a dowhile loop? but I can't get the function to sit right within it, help please?