Php two-dimensional array sorting array_multisort

Source: Internet
Author: User
Php two-dimensional array sorting array_multisort is a common problem in sorting two-dimensional arrays or multi-dimensional arrays. in php, we have a special multi-dimensional array sorting function. Below is a brief introduction:

Array_multisort (array1, sorting order, sorting type, array2, array3.) is a function for sorting multiple arrays or multi-dimensional arrays.

Array1 Required. Specifies the input array.
Sorting order Optional. Specify the order. Possible values are SORT_ASC and SORT_DESC.
Sorting type Optional. Specifies the sorting type. Possible values are SORT_REGULAR, SORT_NUMERIC, and SORT_STRING.
Array2 Optional. Specifies the input array.
Array3 Optional. Specifies the input array.

The array in the parameter is treated as a table column and sorted BY rows-similar to the SQL ORDER BY clause function. The first array is the main array to be sorted. If the rows (values) in the array are the same, they are sorted according to the corresponding values in the next input array.

The first parameter is an array, and each subsequent parameter may be an array or one of the following sort order signs (the sort mark is used to change the default sort order:

  • SORT_ASC-default, in ascending order. A-Z)
  • SORT_DESC-sort in descending order. Z-A)
  • You can then specify the sorting type:

  • SORT_REGULAR-default. Sort each item in the general order.
  • SORT_NUMERIC-sort each item in numerical order.
  • SORT_STRING-sort each item in alphabetical order

  •  1,'name'=>'fj','weight'=>100,'height'=>180),array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170));var_dump($person);$person = my_sort($person,'name',SORT_ASC,SORT_STRING);var_dump($person);$person = my_sort($person,'weight');var_dump($person);?>

    The result is as follows:

    array (size=7)  0 =>     array (size=4)      'id' => int 1      'name' => string 'fj' (length=2)      'weight' => int 100      'height' => int 180  1 =>     array (size=4)      'id' => int 2      'name' => string 'tom' (length=3)      'weight' => int 53      'height' => int 150  2 =>     array (size=4)      'id' => int 3      'name' => string 'jerry' (length=5)      'weight' => int 120      'height' => int 156  3 =>     array (size=4)      'id' => int 4      'name' => string 'bill' (length=4)      'weight' => int 110      'height' => int 190  4 =>     array (size=4)      'id' => int 5      'name' => string 'linken' (length=6)      'weight' => int 80      'height' => int 200  5 =>     array (size=4)      'id' => int 6      'name' => string 'madana' (length=6)      'weight' => int 95      'height' => int 110  6 =>     array (size=4)      'id' => int 7      'name' => string 'jordan' (length=6)      'weight' => int 70      'height' => int 170
    array (size=7)  0 =>     array (size=4)      'id' => int 4      'name' => string 'bill' (length=4)      'weight' => int 110      'height' => int 190  1 =>     array (size=4)      'id' => int 1      'name' => string 'fj' (length=2)      'weight' => int 100      'height' => int 180  2 =>     array (size=4)      'id' => int 3      'name' => string 'jerry' (length=5)      'weight' => int 120      'height' => int 156  3 =>     array (size=4)      'id' => int 7      'name' => string 'jordan' (length=6)      'weight' => int 70      'height' => int 170  4 =>     array (size=4)      'id' => int 5      'name' => string 'linken' (length=6)      'weight' => int 80      'height' => int 200  5 =>     array (size=4)      'id' => int 6      'name' => string 'madana' (length=6)      'weight' => int 95      'height' => int 110  6 =>     array (size=4)      'id' => int 2      'name' => string 'tom' (length=3)      'weight' => int 53      'height' => int 150
    array (size=7)  0 =>     array (size=4)      'id' => int 2      'name' => string 'tom' (length=3)      'weight' => int 53      'height' => int 150  1 =>     array (size=4)      'id' => int 7      'name' => string 'jordan' (length=6)      'weight' => int 70      'height' => int 170  2 =>     array (size=4)      'id' => int 5      'name' => string 'linken' (length=6)      'weight' => int 80      'height' => int 200  3 =>     array (size=4)      'id' => int 6      'name' => string 'madana' (length=6)      'weight' => int 95      'height' => int 110  4 =>     array (size=4)      'id' => int 1      'name' => string 'fj' (length=2)      'weight' => int 100      'height' => int 180  5 =>     array (size=4)      'id' => int 4      'name' => string 'bill' (length=4)      'weight' => int 110      'height' => int 190  6 =>     array (size=4)      'id' => int 3      'name' => string 'jerry' (length=5)      'weight' => int 120      'height' => int 156

    The key here is to save the key to be sorted to a one-dimensional array, and then you can use the array_multisort () function to sort the array by key. of course, the sorting here is not applicable to the array_multisort () function, which can be achieved only through foreach traversal. However, since php developers provide us with a better solution, we can save unnecessary trouble.

    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.