Sort functions for PHP

Source: Internet
Author: User
Sort (): Simple sorting of an array element from low to high, this function can be arranged numerically or alphabetically.

Listing A
   
    $data = array(5,8,1,7,2);  sort($data);  print_r($data);?>输出结果如下所示:Array ([0] => 1[1] => 2[2] => 5[3] => 7[4] => 8)

Rsort (): An array of elements are inverted from high to low, and can be arranged numerically or alphabetically.


   
    $data = array(5,8,1,7,2);rsort($data); print_r($data);?>它的输出结果如下:Array ([0] => 8[1] => 7[2] => 5[3] => 2[4] => 1)

Ksort (): a function that sorts according to a keyword, while maintaining the relevance of the keyword during sorting.


   
    $data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");    ksort($data);    print_r($data);?>它的输出结果如下:Array ([DE] => Germany[ES] => Spain[IN] => India[US] => United States)

Krsort (): An array of keywords is inverted according to the keyword.


   
    $data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");    krsort($data);    print_r($data);?>它的输出结果如下:Array ([US] => United States[IN] => India[ES] => Spain[DE] => Germany)

Asort (): Use value sorting to replace keyword ordering, note that the link between the keyword-value is always maintained.


   
    $data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");    asort($data);    print_r($data);?>它的输出结果如下:Array ([DE] => Germany[IN] => India[ES] => Spain[US] => United States)

Arsort (): Inverted alphabetically by value.


   
    $data = array("US" => "United States", "IN" => "India", "DE" => "Germany", "ES" => "Spain");    arsort($data);    print_r($data);?>Array ([US] => United States[ES] => Spain[IN] => India[DE] => Germany)

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP sorting function, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.