Differences between php array sorting array_multisort and uasort

Source: Internet
Author: User

Example: (concise)
Uasort ($ arr, create_function ('$ a, $ B ', 'Return $ a [\ 'line _ num \ '] <$ B [\ 'line _ num \']; ');
* ************ Function Definition and syntax *************
Array_multisort
(PHP4> = 4.0b4)
Array_multisort --- sort arrays of compound or diverse sizes
Syntax: bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])
Note:
Array_multisort () can be used to immediately sort several arrays or multi-dimensional arrays.
The input array is regarded as the column position of the table and is sorted BY column (rows). This is similar to the SQL ORDER BY clause function. The first array is the first column (primary) sort array. The columns (values) in this array are sorted in the same order as the next input array.
The index structure of this function is unique (unusual) bit, but it is flexible. The first quote must be an array, and the subsequent quote can be an array or one of the sorting flag of the next list.
Sort order flag:
SORT_ASC-sort in ascending order
SORT_DESC-sort in descending order
Sort type flag:
SORT_REGULAR-normal comparison items
SORT_NUMERIC-compare items with numbers
SORT_STRING-compare items as strings
Two flag of the same type cannot be used to specify the flag after each array, and the sorted flag is specified after the array reference, only for this array, otherwise, the preset SORT_ASC and SORT_REGULAR values are reset after the array arguments.
If the request succeeds, true is returned. If the request fails, false is returned.
* ************ Function Definition and syntax *************
Uasort ()
The function uses the custom comparison function to sort the array and maintain the index Association (no new key is assigned to the element ).
If the call succeeds, TRUE is returned. Otherwise, FALSE is returned.
This function is mainly used to sort the arrays that are very important to the unit order.
Syntax
Uasort (array, sorttype) parameter description
Array is required. Specifies the array to be sorted.
Required. User-defined functions.
The function must be designed to return-1, 0, or 1, and should accept two parameters for comparison and work in a way similar to the following:
If a = B, 0 is returned.
If a <B, 1 is returned.
If a> B,-1 is returned.
PHP uasort () function
Definition and usage
The uasort () function uses the User-Defined comparison function to sort the array and maintain the index Association (no new key is assigned to the element ).
If the call succeeds, TRUE is returned. Otherwise, FALSE is returned.
This function is mainly used to sort the arrays that are very important to the unit order.
Syntax
Uasort (array, sorttype) parameter description
Array is required. Specifies the array to be sorted.
Required. User-defined functions.
The function must be designed to return-1, 0, or 1, and should accept two parameters for comparison and work in a way similar to the following:
If a = B, 0 is returned.
If a <B, 1 is returned.
If a> B,-1 is returned.
Example Copy codeThe Code is as follows: <? Php
Function my_sort ($ a, $ B)
{
If ($ a = $ B) return 0;
Return ($ a> $ B )? -1: 1;
}
$ People = array ("Swanson" => "Joe ",
"Griffin in" => "Peter", "Quagmire" => "Glenn ",
"Swanson" => "joe", "griffin" => "peter ",
"Quagmire" => "glenn ");
Uasort ($ people, "my_sort ");
Print_r ($ people );
?>

Output:Copy codeThe Code is as follows: Array
(
[Griffin in] => peter
[Swanson] => joe
[Quagmire] => glenn
[Griffin in] => Peter
[Swanson] => Joe
[Quagmire] => Glenn
)

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.