Simple Example of sorting two-dimensional arrays by php

Source: Internet
Author: User

Simple Example of sorting two-dimensional arrays by php

This article mainly introduces a simple example of php sorting two-dimensional arrays. If you need it, please refer to it.

This article describes how to use the array_multisort function in php to sort two-dimensional arrays. For more information, see. Following the previous article: PHP two-dimensional array sorting custom function, today we will introduce another example of php two-dimensional array sorting.

The sorting of two-dimensional arrays in php is very simple. The array_multisort function is mainly used.

Example:

 

The Code is as follows:

<? Php

/**

* Php 2D array sorting

* Edit www.jb51.net

*/

$ 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

)

)

 

 

 

 

Note:

The parameters of the array_multisort function are flexible. For details, refer to the instructions in the php manual.

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.