The difference _php skill between array_multisort and Uasort in PHP array sorting

Source: Internet
Author: User
Tags mixed
Example: (Concise)
Uasort ($arr, create_function (' $a, $b ', ' return $a [\ ' line_num\ ']< $b [\ ' line_num\ '];]);
function definitions and Syntax *************
Array_multisort
(PHP4 >= 4.0b4)
Array_multisort---Sort a composite or multiple array of dimensions
Syntax: bool Array_multisort (Array ar1 [, mixed arg [, mixed ... [, array ...]]
Description
Array_multisort () can be used to sort a number of arrays or multiple dimensions (multi-dimensional) arrays immediately.
The input array is considered to be the field of the table. Sort it by column (rows), which is similar to the SQL ORDER BY clause function, the first array is the first (primary) sorted array, and the column (value) in this array is the same sort of array as the one entered.
The argument structure of this function is a unique (unusual) bit, but is flexible. The first argument must be an array, and then the argument can be one of the array or the sort flag (flag) of the next list.
Sort order Flag:
SORT_ASC-Sort into ascending order
Sort_desc-Sort into descending order
Sort Type flag:
Sort_regular-Normal comparison project
Sort_numeric-With a digital comparison project
Sort_string-as a string to compare items
You cannot use two identical flags to specify that after each array, the sorted flag is specified after the array argument, only for this array, and the others are reset to the preset sort_asc and sort_regular after the array argument.
Success returns TRUE, and failure returns false.
function definitions and Syntax *************
Uasort ()
The function uses the user-defined comparison function to sort the array and maintains the index association (not assigning a new key to the element).
Returns TRUE if successful, otherwise returns FALSE.
This function is primarily used to sort the associative arrays that are important to the order of the cells.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specify the array to sort.
function 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 manner similar to the following:
If a = B, return 0
If a < B, return 1
If a > B, return-1
PHP Uasort () function
Definitions and usage
The Uasort () function sorts the array by using the user-defined comparison function and keeps the index associated (not assigning a new key to the element).
Returns TRUE if successful, otherwise returns FALSE.
This function is primarily used to sort the associative arrays that are important to the order of the cells.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specify the array to sort.
function 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 manner similar to the following:
If a = B, return 0
If a < B, return 1
If a > B, return-1
Example
Copy Code code as follows:

<?php
function My_sort ($a, $b)
{
if ($a = = $b) return 0;
return ($a > $b)? -1:1;
}
$people = Array ("Swanson" => "Joe"),
"Griffin" => "Peter", "Quagmire" => "Glenn",
"Swanson" => "Joe", "Griffin" => "Peter",
"Quagmire" => "Glenn");
Uasort ($people, "My_sort");
Print_r ($people);
?>

Output:
Copy Code code as follows:

Array
(
[Griffin] => Peter
[Swanson] => Joe
[Quagmire] => Glenn
[Griffin] => Peter
[Swanson] => Joe
[Quagmire] => Glenn
)
Related Article

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.