krsort

(PHP 4, PHP 5)

krsortSort an array by key in reverse order

Description

bool krsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

Sorts an array by key in reverse order, maintaining key to data correlations. This is useful mainly for associative arrays.

Parameters

array

The input array.

sort_flags

You may modify the behavior of the sort using the optional parameter sort_flags, for details see sort().

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 krsort() example

<?php
$fruits 
= array("d"=>"lemon""a"=>"orange""b"=>"banana""c"=>"apple");
krsort($fruits);
foreach (
$fruits as $key => $val) {
    echo 
"$key = $val\n";
}
?>

The above example will output:

d = lemon
c = apple
b = banana
a = orange

See Also

Коментарии

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);
}
2003-11-07 15:51:35
http://php5.kiev.ua/manual/ru/function.krsort.html
Автор:
To create a natural reverse sorting by keys, use the following function:

<?php
function natkrsort($array
{
   
$keys array_keys($array);
   
natsort($keys);

    foreach (
$keys as $k)
    {
       
$new_array[$k] = $array[$k];
    }
   
   
$new_array array_reverse($new_arraytrue);

    return 
$new_array;
}
?>
2005-08-07 21:37:41
http://php5.kiev.ua/manual/ru/function.krsort.html

    Поддержать сайт на родительском проекте КГБ