note 37261 added to function.krsort
| From: | peter at pmkmedia dot com | Date: | Fri, 07 Nov 2003 18:51:35 +0000 |
| Subject: | note 37261 added to function.krsort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60113@lists.php.net to get a copy of this message | ||
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);
}
----
Manual Page -- http://www.php.net/manual/en/function.krsort.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37261
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37261&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37261&report=yes
Search -- http://master.php.net/manage/user-notes.php