Sorting arrays of PHP arrays by multidimensional array

Source: Internet
Author: User
Tags sorts
Sorting arrays of PHP arrays by multidimensional array

The most powerful type in PHP is a non-array! Any type of data can be stored in an array.

In the project development, the sorting of the array is a commonplace, this article mainly summarizes some experience and methods and their differences, for everyone to learn the reference.

One-dimensional array sorting

A, sort function:

This function converts an array value to an integer and is rescheduled from lowest to highest , removing the original key name. Returns TRUE on success, or FALSE on failure.

B, Asort function:

This function is low- to-high -order, the index of the array is persisted, and the cell is associated . It is primarily used to sort the associative arrays that are important for the cell order.

C, Arsort function: bool Arsort(Array & $array [, int $sort _flags])

reverse-Sort an array (high to low) and keep the index relationship

This function sorts the arrays, the index of the array remains, and the cell is associated . It is primarily used to sort the associative arrays that are important for the cell order.

Returns TRUE on success, or FALSE on failure.


Multidimensional array sorting

A, Usort function: bool Usort(array& $array, callback$cmp_function)

This function will use a user-defined comparison function to sort the values in an array. This function should be used if the array to be sorted needs to be sorted with an unusual standard.

The comparison function must return an integer less than, equal to, or greater than 0 when the first argument is considered less than, equal to, or greater than the second argument

This function assigns the new key name to the elements in the array. This will delete the original key name instead of just reordering the key name.

Returns TRUE on success, or FALSE on failure.

B, Uasort function: bool Uasort(Array & $array, callback$cmp_function)

Uasort? Use a user-defined comparison function to sort the values in the array and keep the index associated

This function sorts the array and maintains the association between the index and the cell . It is primarily used to sort the associative arrays that are important for the cell order.

The comparison function is user-defined.

Returns TRUE on success, or FALSE on failure.

C, Uksort function: bool Uksort(Array & $array, callback$cmp_function)

This function uses the user-provided comparison function to sort the key names in the array. This function should be used if the array to be sorted needs to be sorted with an unusual standard.

The Cmp_function function should accept two parameters, which will be populated with a pair of key names in the array.

The comparison function must return an integer less than 0, equal to zero, or greater than 0 when the first parameter is less than, equal to, or greater than the second argument.

Returns TRUE on success, or FALSE on failure (this sort method is very similar to uasort).

C, Array_multisort function: bool Array_multisort(Array$ar1 [, Mixed$arg [, mixed$ ... [, array$ ...]] )

Can be used to sort multiple arrays at once, or to sort multidimensional arrays based on one dimension or multidimensional.

The association (string) key name remains the same, but the numeric key name is re-indexed.

The parameter structure of this function is somewhat unusual, but very flexible.

The first argument must be an array. Each of the following parameters can be an array or a sort flag listed below.

Sort order Flags:

? Sort_asc-Sort by ascending order

? Sort_desc-Sort by descending order

Sort Type flag:

? Sort_regular-Compare items by usual method

? Sort_numeric-Compare items by value

? Sort_string-You cannot specify two similar sort flags after each array is compared by string.

The sort flags specified after each array are valid only for the array-before this is the default value Sort_asc and Sort_regular.


Uasort Sort Study cases:

/* Multidimensional Array sorting */$arr _more = Array (Array (1, ' age ' = ' + ',         ' name ' = ' wzq '),         array (2, ' age ' = ", ' name ' = = ') Eee '),         Array (9, ' age ' = +, ' name ' = ' + '),         Array (4, ' age ' = 162, ' name ' = = ' ff '),         Array (6, ' Age ' = 2, ' name ' = ' JJ '),         Array (' age ' = ', ' name ' = ' nn ')); Uasort ($arr _more,function ($x, $y) {     //age from the big to the small      return $x [' age '] < $y [' old '];}); Var_dump ($arr _more);

These are the above, and the knowledge points are taken from the PHP learning manual. I hope we have some help!

  • 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.