Php one-dimensional two-dimensional array key sorting method example Summary

Source: Internet
Author: User
This article mainly introduces the php one-dimensional two-dimensional array key sorting method, summarizes the bubble sorting method for one-dimensional arrays in the form of examples, and uses array_multisort () to sort two-dimensional arrays by specified key values, it has good reference value. if you need it, you can refer to the example below to summarize the one-dimensional two-dimensional array key sorting method of php. Share it with you for your reference. The specific method is as follows:

In php, array sorting has always been a common issue. next we will focus on the implementation program for sorting one-dimensional arrays and two-dimensional arrays in php, I believe it provides some reference for you.

Function: sorts arrays again.

(Bubble sort (one-dimensional array) (a two-dimensional array of a healthy sorting)

Compare the size of the elements to be sorted. if the two data elements are in the opposite order, they are exchanged until there is no reverse order of the data elements.

Imagine the sorted array R [1 .. n] Vertical vertical erect, each data element is considered as a bubble with weight, scanning the array from bottom up, where a light bubble that violates the principle is scanned, it will "float" up ". this is repeated. until the last two of the above are light, the severe ones.

The code is as follows:

/**
* Bubble sort (one-dimensional array)
* Compare the size of the elements to be sorted. if the two data elements are in the opposite order, they are exchanged until there is no data element in the reverse order.
* Imagine the sorted array R [1 .. n] Vertical vertical erect, each data element is considered as a bubble with weight, scanning the array from bottom up, where a light bubble that violates the principle is scanned, it will "float" up ". this is repeated.
* Until the last two parties are put on the lighter ones, the heavier ones will be placed on the lighter ones.
*/
Function bubble_sort ($ array, $ key = null ){
$ Count = count ($ array );
If ($ count <0 ){
Return false;
}
For ($ I = 0; $ I <$ count; $ I ++ ){
For ($ j = $ count-1; $ j> $ I; $ j --){
If ($ key & isset ($ array [$ key]) {// Two-dimensional array Jian existence
If ($ array [$ j] [$ key] <$ array [$ j-1] [$ key]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ tmp;
}
} Else {// one-dimensional array
If ($ array [$ j] <$ array [$ j-1]) {
$ Tmp = $ array [$ j];
$ Array [$ j] = $ array [$ j-1];
$ Array [$ j-1] = $ tmp;
}
}
}
}
Return $ array;
}

How to sort array_multisort

How to use array_multisort () to sort two arrays by specified key values

In this example, a two-dimensional array named $ data is defined and sorted as follows:

The code is as follows:

$ 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'];
}

// Sort data by volume in descending order and edition in ascending order
// Use $ data as the last parameter and sort it by a common key
Array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data );
Print_r ($ data );

The output is as follows:

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

We only need to use sort () to sort one-dimensional arrays. the corresponding asort ($ arr) function is to sort key values and maintain the original key-value relationship.

In the same principle, rsort (); arsort (); krsort (); functions are sorted in descending order, and other functions include sort (); rsort (); ksort (); same.

I hope this article will help you with PHP programming.

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.