PHP two-dimensional array sorting methods and user-defined functions

Source: Internet
Author: User
This article mainly introduces three methods of PHP 2D array sorting and user-defined functions. For more information, see

This article mainly introduces three methods of PHP 2D array sorting and user-defined functions. For more information, see

Generally, sorting is performed by sorting the database or nosql (eg: redis) and then outputting it to the program. However, sometimes we need to sort the array directly through PHP, objects and arrays are the most used for storing data in PHP, but arrays are the most used for processing, because there are rich built-in function libraries (in fact, objects can also be understood as arrays to a certain extent), these function libraries can greatly help us implement some functions. Common system functions include sort, asort, arsort, ksort, and krsort. Here I mainly talk about sorting two-dimensional arrays. There are two methods:

I. sort by using the PHP built-in array_multisort Function

The Code is as follows:


$ Data = array ();
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );

// Retrieve the column list
Foreach ($ data as $ key => $ row)
{
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}

Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );

Print_r ($ data );
?>

Output result:

The Code is as follows:

Array
(
[0] => Array
(
[Volume] => 98
[Edition] => 2
)
[1] => Array
(
[Volume] => 86
[Edition] => 1
)
[2] => Array
(
[Volume] => 86
[Edition] => 6
)
[3] => Array
(
[Volume] => 85
[Edition] => 6
)
[4] => Array
(
[Volume] => 67
[Edition] => 2
)
[5] => Array
(
[Volume] => 67
[Edition] => 7
)
)

The official documents of array_multisort are also described in detail:

Ii. Sorting by user-defined functions 1

The Code is as follows:


$ Data = array ();
$ Data [] = array ('Volume '=> 67, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 1 );
$ Data [] = array ('Volume '=> 85, 'version' => 6 );
$ Data [] = array ('Volume '=> 98, 'version' => 2 );
$ Data [] = array ('Volume '=> 86, 'version' => 6 );
$ Data [] = array ('Volume '=> 67, 'version' => 7 );

// Retrieve the column list
Foreach ($ data as $ key => $ row)
{
$ Volume [$ key] = $ row ['Volume '];
$ Edition [$ key] = $ row ['version'];
}

$ Ret = arraySort ($ data, 'Volume ', 'desc ');

Print_r ($ ret );

/**
* @ Desc arraySort php two-dimensional array sorting sorts the array according to the specified key
* @ Param array $ array to be sorted by arr
* @ Param string $ keys specifies the sorted key
* @ Param string $ type sorting type asc | desc
* @ Return array
*/
Function arraySort ($ arr, $ keys, $ type = 'asc '){
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k =>$ v ){
$ Keysvalue [$ k] = $ v [$ keys];
}
$ Type = 'asc '? Asort ($ keysvalue): arsort ($ keysvalue );
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
}
?>


Output result:

The Code is as follows:

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.