Php array sorting function description and implementation code

Source: Internet
Author: User
Tags foreach sorts

// The sort () function sorts the values of the given array in ascending order.

$ My_array = array ("a" => "dog", "B" => "cat", "c" => "horse ");

Sort ($ my_array );
Print_r ($ my_array );

// The krsort (array, sorttype) function sorts the array by key in reverse order and retains the original key for the array value.
$ My_array = array ("a" => "dog", "B" => "cat", "c" => "horse ");
Krsort ($ my_array );
Print_r ($ my_array );

// View an instance

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;
}

// The arsort () function sorts the array reversely and maintains the index relationship. It is mainly used to sort the arrays that are very important to the unit order.

$ My_array = array ("a" => "dog", "B" => "cat", "c" => "horse ");

Arsort ($ my_array );
Print_r ($ my_array );

// The asort () function sorts the array and maintains the index relationship. It is mainly used to sort the arrays that are very important to the unit order.

$ My_array = array ("a" => "dog", "B" => "cat", "c" => "horse ");

Asort ($ my_array );
Print_r ($ my_array );

// Condense the above instance and write an array sorting function

/**
 
* Sort arrays
* @ Param array $ array operation array
* @ Param string $ type key: Sort by key and value by value
* @ Param string $ field name
* @ Param string $ order sorting method asc order desc reverse order
* @ Return void
*/
Function sort (& $ array, $ type = 'value', $ field = null, $ order = 'asc '){
If ($ field ){
Foreach ($ array as $ key => $ value ){
$ Temp [$ key] = $ value [$ field];
   }
If ($ order = 'asc '){
Asort ($ temp );
} Else {
Arsort ($ temp );
   }
$ Newarray = array ();
Foreach ($ temp as $ key => $ value ){
$ Newarray [] = $ array [$ key];
   }
$ Array = $ newarray;
} Else {
If ($ type = 'key '){
If ($ order = 'asc '){
Ksort ($ array );
} Else {
Krsort ($ array );
    }
} Else {
If ($ order = 'asc '){
Asort ($ array );
} Else {
Arsort ($ array );
    }
   }
  }
 }
  

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.