krsort()函數是PHP中的內置函數,用於根據鍵的索引值以相反的順序對鍵進行排序。它以保持索引和值之間的關係的方式進行排序。
用法:
bool krsort( $array, $sorting_type )
參數:該函數接受上述和以下描述的兩個參數:
- $array:此參數指定要排序的數組。它是必填參數。
 - $sorting_type:這是一個可選參數。下麵討論了不同的排序類型:
- SORT_REGULAR:$sorting_type的值為SORT_REGULAR,然後正常比較各項。
 - SORT_NUMERIC:$sorting_type的值為SORT_NUMERIC,然後對項目進行數值比較。
 - SORT_STRING:$sorting_type的值為SORT_STRING,然後將項目作為字符串進行比較。
 - SORT_LOCALE_STRING:$sorting_type的值為SORT_STRING,然後根據當前語言環境將項目作為字符串進行比較。
 
 
返回值:如果成功,則此函數返回True;如果失敗,則返回False。
以下示例程序旨在說明PHP中的krsort()函數。
程序1:
<?php 
// PHP program to illustrate 
// krsort()function 
  
// Input different array elements 
$arr = array("0" =>"Technology", 
             "1" =>"Machine", 
             "2" =>"GeeksforGeeks", 
             "3" =>"Graphics", 
             "4" =>"Videos", 
             "5" =>"Report", 
             "6" =>"Article", 
             "7" =>"Placement", 
             "8" =>"Contribute", 
             "9" =>"Reset", 
             "10" =>"Copy", 
        ); 
  
// Implementation of krsort() 
krsort($arr); 
  
// for-Loop for displaying result 
foreach ($arr as $key => $val) { 
    echo "[$key] = $val"; 
    echo"\n"; 
} 
  
?>
輸出:
[10] = Copy [9] = Reset [8] = Contribute [7] = Placement [6] = Article [5] = Report [4] = Videos [3] = Graphics [2] = GeeksforGeeks [1] = Machine [0] = Technology
程序2:
<?php 
// PHP program to illustrate 
// krsort function 
      
// Input different array elements 
$arr = array("a" => 11, 
             "b" => 22, 
             "d" => 33, 
             "n" => 44, 
             "o" => 55, 
             "p" => 66, 
             "r" => 77, 
             "s" => 2, 
             "q" => -11, 
             "t" => 3, 
             "u" => 1000, 
             "z" => 1,                             
        ); 
// Implementation of krsort 
krsort($arr); 
  
// for-Loop for displaying result 
foreach ($arr as $key => $val) { 
    echo "[$key] = $val"; 
    echo"\n"; 
} 
  
?>
輸出:
[z] = 1 [u] = 1000 [t] = 3 [s] = 2 [r] = 77 [q] = -11 [p] = 66 [o] = 55 [n] = 44 [d] = 33 [b] = 22 [a] = 11
相關文章:
參考: http://php.net/manual/en/function.krsort.php
相關用法
- d3.js d3.hcl()用法及代碼示例
 - PHP abs()用法及代碼示例
 - PHP Ds\Map put()用法及代碼示例
 - PHP sin( )用法及代碼示例
 - PHP cos( )用法及代碼示例
 - d3.js d3.lab()用法及代碼示例
 - PHP exp()用法及代碼示例
 - d3.js d3.map.set()用法及代碼示例
 - PHP next()用法及代碼示例
 - PHP pow( )用法及代碼示例
 - PHP pi( )用法及代碼示例
 - d3.js d3.mean()用法及代碼示例
 
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | krsort() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
