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

Source: Internet
Author: User

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
Copy codeThe Code is as follows:
<? Php

$ 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:

Copy codeThe 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
)
)

About array_multisort official documentation also has a more detailed description: http://www.php.net/manual/zh/function.array-multisort.php

Ii. Sorting by user-defined functions 1
Copy codeThe Code is as follows:
<? Php
$ 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:
Copy codeThe Code is as follows: Array
(
[3] => Array
(
[Volume] => 98
[Edition] => 2
)

[4] => Array
(
[Volume] => 86
[Edition] => 6
)

[1] => Array
(
[Volume] => 86
[Edition] => 1
)

[2] => Array
(
[Volume] => 85
[Edition] => 6
)

[5] => Array
(
[Volume] => 67
[Edition] => 7
)

[0] => Array
(
[Volume] => 67
[Edition] => 2
)

)

The difference between this UDF and the system function is that the UDF only supports sorting of a specific key. If you want to support sorting of multiple keys, you need to execute it multiple times; the system function array_multisort can specify multiple sorting rules for multiple keys at a time. The system function is quite powerful. We recommend that you use system functions. After all, it is implemented at the underlying level of C, here is just an example to illustrate how to sort the array through a user-defined function. Of course, this user-defined function can be expanded to support more sorting rules. It is used in many scenarios, such as rankings, rankings, and scores.

Iii. Sorting of user-defined functions 2

The following function sorts a given two-dimensional array by the specified key value. First, let's look at the function definition:
Copy codeThe Code is as follows:
Function array_sort ($ arr, $ keys, $ type = 'asc '){
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k =>$ v ){
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc '){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
}
It can sort two-dimensional arrays by specified key values, or specify the ascending or descending sort method (the default is ascending). Usage example:
Copy codeThe Code is as follows:
$ Array = array (
Array ('name' => 'phone', 'brand' => 'nokia ', 'price' => 1050 ),
Array ('name' => 'laptop ', 'brand' => 'lenovo', 'price' => 4300 ),
Array ('name' => 'shares', 'brand' => 'phillips', 'price' => 3100 ),
Array ('name' => 'treadmill ', 'brand' => 'three', 'price' => 4900 ),
Array ('name' => 'Watch ', 'brand' => 'case', 'price' => 960 ),
Array ('name' => 'LCD TV', 'brand' => 'sony ', 'price' => 6299 ),
Array ('name' => 'laser printer ', 'brand' => 'hp', 'price' => 1200)
);

$ ShoppingList = array_sort ($ array, 'price ');
Print_r ($ ShoppingList );

The above orders the two-dimensional array $ array in descending order of 'price.

 

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.