PHP Basics-Two-d array ordering sister articles

Source: Internet
Author: User

The previous description of the PHP multidimensional array sort of a function array_multisort (), want to know people can click

Two-dimensional array sorting array_multisort


The following describes the sorting of multidimensional arrays that do not apply to Array_multisort ().

Here are the next 2 php sorting functions, one is asort, and the other is arsort.

The Asort (array,sorttype) function sorts the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order.

The Second optional parameter contains an additional sort ID.

    • Sort_regular-Default. Processed in their original type (without changing the type).
    • Sort_numeric-Handle the value as a number
    • Sort_string-handles the value as a string
    • Sort_locale_string-handles the value as a string, based on local settings.

Returns TRUE if successful, otherwise FALSE.


The Arsort (array,sorttype) function reverses the array and maintains the index relationship. It is primarily used to sort the associative arrays that are important for the cell order. This function is used in the same way as Asort, except that this is an inverse sort of the index of the array.


The sorting algorithm for two-dimensional arrays is given below:

<?php         function Array_sort ($arr, $key, $type = ' asc ') {$keyvalues = $new _array = Array (); foreach ($arr as $k + = $v) { $keyvalues [$k] = $v [$key];} if ($type = = ' asc ') {asort ($keyvalues);} Else{arsort ($keyvalues);} foreach ($keyvalues as $k = + $v) {$temparray [$k] = $arr [$k];} return $temparray; } $student = Array (' name ' = ' Zhang San ', ' age ' = ', ' weight ' =>100, ' height ' =>180), Array (' name ' = = ' John Doe ', ' Age ' = ' + ', ' weight ' =>200, ' height ' =>150), Array (' name ' = ' Harry ', ' age ' = '-', ' weight ' =>150, ' Height ' =>165 ', Array (' name ' = ' = ' Zhao Liu ', ' age ' = ' + ', ' weight ' =>90, ' height ' =>173), Array (' name ' = ' Sun seven ', ' Age ' = ' + ', ' weight ' =>160, ' height ' =>170); $studentlist = Array_sort ($student, ' weight '); Var_dump ($ Studentlist);




by Asort This function, the index ordering of the two-dimensional array is preserved, and then the index corresponding to the sorted two-dimensional array is obtained.

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.