PHP two-dimensional array sorting array_multisort

Source: Internet
Author: User

For a 2-dimensional array or multi-dimensional array sorting is a common problem, in PHP we have a special multi-dimensional array sorting function, the following simple introduction:

Array_multisort (array1,sorting order, sorting type,array2,array3. ) is a function that sorts multiple arrays or multidimensional arrays.

Array1 Necessary. Specifies the input array.
Sorting order Optional. Specify the order of arrangement. The possible values are SORT_ASC and Sort_desc.
Sorting type Optional. Specifies the sort type. The 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 column of a table and sorted by rows-this is similar to the function of the SQL ORDER by clause. The first array is the primary array to sort. If the rows (values) in the array are the same, they are sorted according to the size of the corresponding values in the next input array, and so on.

The first parameter is an array, and each subsequent argument may be an array, or it may be one of the following sort order flags (the sort flags are used to change the default order):

    • SORT_ASC-By default, in ascending order. (A-Z)
    • Sort_desc-sorted in descending order. (Z-A)

You can then specify the sort type:

    • Sort_regular-Default. Arranges each item in a regular order.
    • Sort_numeric-Sorts each item in numerical order.
    • Sort_string-Alphabetical order of each item

<?phpfunction My_sort ($arrays, $sort _key, $sort _order=sort_asc, $sort _type=sort_numeric) {if (Is_array ($arrays)) { foreach ($arrays as $array) {if (Is_array ($array)) {$key _arrays[] = $array [$sort _key];} else{return false;}} }else{return false;} Array_multisort ($key _arrays, $sort _order, $sort _type, $arrays); return $arrays; } $person = array (' ID ' =>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 ' = ), 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 results are as follows:
Array (size=7)0=     Array (size=4)' ID '= int 1' Name '= string ' FJ ' (length=2)' Weight '= int  -' Height '= int  the1=     Array (size=4)' ID '= int 2' Name '= string ' Tom ' (length=3)' Weight '= int  -' Height '= int  Max2=     Array (size=4)' ID '= int 3' Name '= string ' Jerry ' (length=5)' Weight '= int  -' Height '= int 1563=     Array (size=4)' ID '= int 4' Name '= string ' Bill ' (length=4)' Weight '= int  the' Height '= int  the4=     Array (size=4)' ID '= int 5' Name '= string ' linken ' (length=6)' Weight '= int  the' Height '= int  $5=     Array (size=4)' ID '= int 6' Name '= string ' Madana ' (length=6)' Weight '= int  the' Height '= int  the6=     Array (size=4)' ID '= int 7' Name '= string ' Jordan ' (length=6)' Weight '= int  -' Height '= int  the
Array (size=7)0=     Array (size=4)' ID '= int 4' Name '= string ' Bill ' (length=4)' Weight '= int  the' Height '= int  the1=     Array (size=4)' ID '= int 1' Name '= string ' FJ ' (length=2)' Weight '= int  -' Height '= int  the2=     Array (size=4)' ID '= int 3' Name '= string ' Jerry ' (length=5)' Weight '= int  -' Height '= int 1563=     Array (size=4)' ID '= int 7' Name '= string ' Jordan ' (length=6)' Weight '= int  -' Height '= int  the4=     Array (size=4)' ID '= int 5' Name '= string ' linken ' (length=6)' Weight '= int  the' Height '= int  $5=     Array (size=4)' ID '= int 6' Name '= string ' Madana ' (length=6)' Weight '= int  the' Height '= int  the6=     Array (size=4)' ID '= int 2' Name '= string ' Tom ' (length=3)' Weight '= int  -' Height '= int  Max
Array (size=7)0=     Array (size=4)' ID '= int 2' Name '= string ' Tom ' (length=3)' Weight '= int  -' Height '= int  Max1=     Array (size=4)' ID '= int 7' Name '= string ' Jordan ' (length=6)' Weight '= int  -' Height '= int  the2=     Array (size=4)' ID '= int 5' Name '= string ' linken ' (length=6)' Weight '= int  the' Height '= int  $3=     Array (size=4)' ID '= int 6' Name '= string ' Madana ' (length=6)' Weight '= int  the' Height '= int  the4=     Array (size=4)' ID '= int 1' Name '= string ' FJ ' (length=2)' Weight '= int  -' Height '= int  the5=     Array (size=4)' ID '= int 4' Name '= string ' Bill ' (length=4)' Weight '= int  the' Height '= int  the6=     Array (size=4)' ID '= int 3' Name '= string ' Jerry ' (length=5)' Weight '= int  -' Height '= int 156

the point here is that the key to be sorted is stored in a one-dimensional array, and then you can use the Array_multisort () function to sort the array by key, of course, the sort you can not apply to Array_multisort () This function can only be achieved with a foreach traversal, but since PHP developers have provided us with a better approach, we can eliminate unnecessary hassles.

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.