Php array sorting-multi-dimensional array sorting-php Tutorial

Source: Internet
Author: User
Php array sorting-multi-dimensional array sorting-Php array sorting-multi-dimensional array sorting

The most powerful non-array type in php! Data of any type can be stored in arrays.

Array sorting is a common practice in project development. This article mainly summarizes some experiences, methods, and their differences for your reference.

One-dimensional array sorting functions a and sort:

This function is used to replace the array value with an integer andMinimum to maximumReschedule and delete the original key name. Returns TRUE if the call succeeds, or FALSE if the call fails.

B. asort functions:

This function performsLow to HighSort, arrayIndex persistence and unit Association. It is mainly used to sort the arrays that are very important to the unit order.

C. arsort function: bool Arsort(Array & $ array [, int $ sort_flags])

PerformReverse sorting (high to low)And maintain the index relationship

This function sorts the array and indexes the array.Maintain association with units. It is mainly used to sort the arrays that are very important to the unit order.

Returns TRUE if the call succeeds, or FALSE if the call fails.


Multi-dimensional array sorting a and usort functions: bool Usort(Array & $ array, callback $ cmp_function)

This function uses a user-defined comparison function to sort the values in an array. If the array to be sorted needs to be sorted by an unusual standard, use this function.

When the first parameter is considered to be less than, equal to, or greater than the second parameter, the comparison function must return an integer that is less than, equal to, or greater than zero.

This function is used to assign a new key name to the element in the array. This willDelete the original key nameInstead of simply sorting the key names.

Returns TRUE if the call succeeds, or FALSE if the call fails.

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

Uasort? Sort the values in the array using the user-defined comparison function and maintain the index Association

This function sorts the array andMaintain the association between indexes and units. It is mainly used to sort the arrays that are very important to the unit order.

Comparison functions are user-defined.

Returns TRUE if the call succeeds, or FALSE if the call fails.

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

This function uses the comparison function provided by the user to sort the key names in the array. If the array to be sorted needs to be sorted by an unusual standard, use this function.

The cmp_function function should accept two parameters, which will be filled by a pair of key names in the array.

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

Returns TRUE if the command is successful or FALSE if the command fails (this sorting method is similar to uasort ).

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

It can be used to sort multiple arrays at a time, or to sort multi-dimensional arrays based on one or more dimensions.

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

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

The first parameter must be an array. Each of the following parameters can be an array or a sorting marker listed below.

Sort order mark:

? SORT_ASC-sort by ascending order

? SORT_DESC-sort by descent

Sorting type flag:

? SORT_REGULAR-compare projects by common methods

? SORT_NUMERIC-compare projects by numerical values

? SORT_STRING-after comparing an item by string, you cannot specify two sort flags of the same type.

The sorting flag specified after each array is only valid for this array-the default values are SORT_ASC and SORT_REGULAR.


Uasort sorting case study:

/* Multi-dimensional array sorting */$ arr_more = array (1, 'age' => 12, 'name' => 'wzq'), array (2, 'age' => 42, 'name' => 'eee'), array (9, 'age' => 82, 'name' => '22 '), array (4, 'age' => 162, 'name' => 'ff'), array (6, 'age' => 2, 'name' => 'JJ '), array (17, 'age' => 52, 'name' => 'nn'); uasort ($ arr_more, function ($ x, $ y) {// age from large to small return $ x ['age'] <$ y ['age'];}); var_dump ($ arr_more );

The above is the knowledge point from the php Learning Manual. Hope to help you!

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.