Php one-dimensional array sorting and multi-dimensional array sorting _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php one-dimensional array sorting and multi-dimensional array sorting. Php Tutorial one-dimensional array sorting and multi-dimensional array sorting first look at the one-dimensional data sorting instance asort () function and ksort () function? The phpasort () function is in ascending order of array values. $ mixarray (Clalei php Tutorial: one-dimensional array sorting and multi-dimensional array sorting

First look at the instance asort () function and ksort () function of one-dimensional data sorting.


// The asort () function is in ascending order of array values.
$ Mix = array ("Clalei" => 10, "Bill" => 50, "Aala" => 100 );
Asort ($ mix );
Print_r ($ mix );
// The ksort () function is in ascending order of the array keywords.
$ Mix = array ("Clalei" => 100, "Bill" => 50, "Aala" => 10 );
Ksort ($ mix );
Print_r ($ mix );

?>

One-dimensional data sorting 2

The sort () function sorts arrays in ascending order of letters or numbers (from low to high:

$ Name = array ("Clalei", "Bill", "Aala ");
Sort ($ name );
For ($ I = 0; $ I <3; $ I ++ ){
Echo $ name [$ I];
}
Echo"
";
$ Price = array (100,50, 10 );
Sort ($ price );
For ($ I = 0; $ I <3; $ I ++ ){
Echo $ price [$ I]. "| ";
}

?>

Now let's take a look at the two-dimensional database tutorial sorting instance code.

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

In this example, we will sort volume in descending order and edition in ascending order.

Now we have an array containing rows, but array_multisort () needs an array containing columns. Therefore, we use the following code to obtain and sort columns.
// 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 );

The data set is sorted as follows:
Volume | edition
------- + --------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7

Two-dimensional array sorting 3

Array_multisort () sorts two-dimensional arrays.
Array $ roughData, which we plan to sort by accurancy.

Array ([0] => Array ([username] => 10yl [accuracy] => 0.00) [1] => Array ([username] => 11yl [accuracy] => 1.00) [2] => Array ([username] => 12yl [accuracy] => 0.00) [3] => Array ([username] => 13yl [accuracy] => 1.00 ))
Method:
(1) extract an array of accuracy columns

Foreach ($ roughData as $ key => $ row ){
$ Usernames [$ key] = $ row ['username'];
$ Accuracy [$ key] = $ row ['accuracy '];
}

(2) sorting
Array_multisort ($ accuracy, SORT_ASC, $ roughData );

When print_r ($ roughData);, we will get a two-dimensional array in ascending order of accuracy.


Let's take a look at a simple example using the two-dimensional array sorting method.

$ Array [] = array ("age" => 20, "name" => "li ");
$ Array [] = array ("age" => 21, "name" => "ai ");
$ Array [] = array ("age" => 20, "name" => "ci ");
$ Array [] = array ("age" => 22, "name" => "di ");

Foreach ($ array as $ key => $ value ){
$ Age [$ key] = $ value ['age'];
$ Name [$ key] = $ value ['name'];
}

Array_multisort ($ age, SORT_NUMERIC, SORT_DESC, $ name, SORT_STRING, SORT_ASC, $ array );
Print_r ($ array );
?>


Sort one-dimensional array sorting and multi-dimensional array sorting first look at one-dimensional data sorting instance asort () function and ksort () function? Php // asort () function in ascending order of array values $ mix = array (Clalei...

Related Article

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.