Interpreting PHP Array sorting

Source: Internet
Author: User
PHP has a powerful library of functions, let's look at the specific usage of each array sort function, the following from the PHP7.0 manual.

    • Sort ()-the array is ordered from low to high, and the function is the root function of all sorting functions; bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) two parameters, except for arrays, the second optional parameter, sort_flags, changes the ordering behavior by an optional type tag.

    
     $auto = array("bens2","honda3","BMW4","aens1","BMW5");sort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[0]=BMW4 auto[1]=BMW5 auto[2]=aens1 auto[3]=bens2 auto[4]=honda3}
    • rsort-the array in reverse order, the parameter list is the same as the sort, the return value is the same as

    
     $auto = array("bens2","honda3","BMW4","aens1","BMW5");rsort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[0]=honda3 auto[1]=BMW5 auto[2]=BMW4 auto[3]=bens2 auto[4]=aens1}
    • Asort ()-Sorts the array and maintains the index relationship, the parameter, the return value ibid., explained in the manual as "this function sorts the array, the index of the arrays is persisted and the cell is associated." It is primarily used to sort the associative arrays that are important for the cell order. "Personally think it and sort () focus on the sorting of associative arrays, and its collation is simply by value, not the key, which is easily distinguished from ksort ().

    
     $auto = array("a"=>"bens2","b"=>"honda3","c"=>"BMW4","d"=>"aens1","e"=>"BMW5");asort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[d]=aens1 auto[a]=bens2 auto[c]=BMW4 auto[e]=BMW5 auto[b]=honda3}
    • Arsort ()-Reverses the order of the array and maintains the index relationship, which is the inverse of the asort ().

    
     $auto = array("a"=>"bens2","b"=>"honda3","c"=>"BMW4","d"=>"aens1","e"=>"BMW5");arsort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[b]=honda3 auto[e]=BMW5 auto[c]=BMW4 auto[a]=bens2 auto[d]=aens1}
    • Ksort ()-the array is sorted by key name, and the manual explains that the array is sorted by key name, preserving the association of the key name to the data. This function is mainly used for associative arrays ", parameter, return value same as sort ()

    
     $auto = array("b"=>"bens2","a"=>"honda3","d"=>"BMW4","c"=>"aens1","e"=>"BMW5");ksort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5}
    • krsort-array is reversed by key name, parameter, return value ibid.

    
     $auto = array("b"=>"bens2","a"=>"honda3","d"=>"BMW4","c"=>"aens1","e"=>"BMW5");ksort($auto,SORT_STRING|SORT_FLAG_CASE);//不区分大小写排序字符串foreach($autoas$key=>$val){    echo"auto[".$key."]=".$val."\n";    //输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5}
    • Natsort-uses the "natural sorting" algorithm to sort the array, the manual explains "This function implements a sort algorithm that is similar to how people usually sort alphanumeric strings and maintains the association of the original key/value, which is called a" natural sort ", and it has a sorting method that differs from the sort (). It has only one parameter that is passed in.

    $auto=Array("B"="Bens2","E"="BENS20","a"="Bens31","D"="Bens41","C"="Bens1","E"="BENS15"); Sort ($auto, sort_string| Sort_flag_case);// case-insensitive sort stringEchothe sort order:
";foreach($auto as$key=$val){Echo"auto[".$key."]=".$val."
";}$auto 1=Array("B"="Bens2","E"="BENS20","a"="Bens31","D"="Bens41","C"="Bens1","E"="BENS15"); Natsort ($auto 1);//parameter has only oneEcho"Natsort Sort:
";foreach($auto 1 as$key=$val){Echo"auto[".$key."]=".$val."
";}//Output/* Sort sorted: Auto[0]=bens1auto[1]=bens15auto[2]=bens2auto[3]=bens31auto[4]=bens41natsort sorted by: auto[c]=bens1auto[ B]=BENS2AUTO[E]=BENS15AUTO[A]=BENS31AUTO[D]=BENS41 * *
    • Shuffle ()-the array is scrambled, it will delete the key name, not just the key name is scrambled. Its argument is only passed in the array variable, the return value is TRUE or False

    
     $auto1 = array("b"=>"bens2","e"=>"bens20","a"=>"bens31","d"=>"bens41","c"=>"bens1","e"=>"bens15");shuffle($auto1);foreach($auto1as$key=>$val){    echo"auto[".$key."]=".$val."
";}//输出/* auto[0]=bens41auto[1]=bens15auto[2]=bens1auto[3]=bens2auto[4]=bens31 */

'). 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 interpretation of the PHP array sorting, 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.