Use PHP to sort a multidimensional array of dimensions.

Source: Internet
Author: User

To order multi-dimensional arrays of PHP, you can use the built-in functions in PHP: Array_multisort ();

Syntax: Array_multisort (array1,sorting order,sorting type,array2,array3 ...)

Parameter description
Array1 required. Specifies the input array.
Sorting order is optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc.
Sorting type is optional. Specifies the sort type. The possible values are Sort_regular, Sort_numeric, and sort_string.
Array2 is optional. Specifies the input array.
Array3 is optional. Specifies the input array.

Instance:

1<?PHP2     $data=Array();//A two-dimensional array to sort3     $randChar= "ABCDEFGHIJKLMNOPQRSTUWVXYZ";4      for($i= 0;$i<100;$i++){5         $randn=Rand(100,999);6         $RANDC=$randChar[Rand(0,25)];7         $data[] =Array($RANDC.$randn,$randn,$randn.$RANDC);8     }9     $num=Array();//The array to sort must be extracted from the $dataTen      for($i= 0;$i<100;$i++){ One         $num[] =$data[$i][2]; A     } -     Array_multisort($num, Sort_numeric,$data);//sorting, sorted by number
- Echo"<pre>"; the    Print_r($data); - Echo"</pre>";

$data will generate a two-dimensional array, $num must be extracted from the $data array.

Personal understanding, although did not see the inside of the code: first use the bubble sort algorithm to arrange a one-dimensional array $num, and then use a for loop to $num with the two-dimensional array $data extracted data to do if judgment. If, for equality, the data is added to the new array. The following larger image can also implement the Array_multisort function

Use PHP to sort a multidimensional array of dimensions.

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.