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

Rsort function:

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

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.

Arsort function:

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 TRUEon success, or FALSEon failure.


Multi-dimensional array sorting 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 TRUEon success, or FALSEon failure.

Uasort function:BOOL Uasort( array& $array , callback$cmp _function )

Uasort - sorts the values in the array using a user-defined comparison function and keeps 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 TRUEon success, or FALSEon failure.

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 TRUEon success, or FALSE on Failure (This sort method is very similar to uasort).

Array_multisort function:BOOL Array_multisort( Array$ar 1 [, 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!


Sorting arrays of PHP arrays by multidimensional array

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.