note 37260 deleted from function.krsort by pollita
| From: | pollita@php.net | Date: | Fri, 07 Nov 2003 18:52:22 +0000 |
| Subject: | note 37260 deleted from function.krsort by pollita | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-60114@lists.php.net to get a copy of this message | ||
Note Submitter: peter@pmkmedia.com
----
Best deal sorting:
This is a function that will sort an array with integer keys (weight) and float values (cost) and
delete 'bad deals' - entries that are more costly than other entries that have greater or
equal weight.
Input: an array of unsorted weight/cost pairs
Output: none
function BEST_DEALS($myarray)
{ // most weight for least cost:
// © Peter Kionga-Kamau, http://www.pmkmedia.com
// thanks to Nafeh for the reversal trick
// free for unrestricted use.
krsort($myarray, SORT_NUMERIC);
while(list($weight, $cost) = each($myarray))
{ // delete bad deals, retain best deals:
if(!$lastweight)
{
$lastweight=$weight;
$lastcost = $cost;
}
else if($cost >= $lastcost) unset($myarray[$weight]);
else
{
$lastweight=$weight;
$lastcost = $cost;
}
}
ksort($myarray);
}