Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: krsort - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ksort" width="11" height="7"/> <key
Last updated: Sun, 23 Sep 2007

view this page in

krsort

(PHP 4, PHP 5)

krsort — 配列をキーで逆順にソートする

説明

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

配列をキーにより逆順にソートします。キーとデータとの関係は維持されます。 この関数は主に連想配列の場合に有用です。

成功した場合に TRUE を、失敗した場合に FALSE を返します。

例 306. krsort() の例

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

上の例の出力は以下となります。


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

    

オプションのパラメータ sort_flags によりソートの動作を修正可能です。詳細については、 sort() を参照ください。

asort()arsort()ksort()sort()natsort() および rsort() も参照ください。



ksort" width="11" height="7"/> <key
Last updated: Sun, 23 Sep 2007
 
add a note add a note User Contributed Notes
krsort
08-Aug-2005 10:37
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_array, true);

    return
$new_array;
}
?>
peter at pmkmedia dot com
08-Nov-2003 06:51
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);
}
lolo at phpheaven dot net
29-Nov-2000 05:33
If you want to emulate the krsort function for an older version of php, you can use this piece of code:

function KeyComp($a, $b)
{  
  return -(strcmp($a,$b));
}

function krsort($MyArray)
{
  uksort($MyArray, "KeyComp");
}

Maybe obvious and useless, but who knows...

ksort" width="11" height="7"/> <key
Last updated: Sun, 23 Sep 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites