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

search for in the

ksort> <key
[edit] Last updated: Sat, 07 Jan 2012

view this page in

krsort

(PHP 4, PHP 5)

krsort키에 의한 배열 역순 정렬

설명

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

역순으로 키에 의해 배열을 정렬하고, 키와 데이터의 연관성을 유지한다. 이 함수는 주로 연관배열에 유용하다.

인수

array

입력 배열.

sort_flags

선택적인 sort_flags 인수로 정렬 방법을 바꿀 수 있습니다. 자세한 내용은 sort()를 참고하십시오.

반환값

성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.

예제

Example #1 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

참고

  • asort() - 배열을 정렬하고 인덱스 상관 관계를 유지
  • arsort() - 배열을 내림차순 정렬하고 인덱스의 상관관계를 유지
  • ksort() - 키에 의한 배열 정렬
  • sort() - 배열 정렬
  • natsort() - "자연순" 알고리즘으로 배열 정렬
  • rsort() - 역순으로 배열 정렬



ksort> <key
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes krsort - [3 notes]
up
1
peter at pmkmedia dot com
9 years ago
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);
}
up
0
Anonymous
7 years ago
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;
}
?>
up
-2
lolo at phpheaven dot net
12 years ago
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...

 
show source | credits | stats | sitemap | contact | advertising | mirror sites