PHP array sorting array_multisort and Uasort differences _php tutorials

Source: Internet
Author: User
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 compound or array of various sizes
Syntax: bool Array_multisort (Array ar1 [, mixed arg [, mixed ... [, array ...]])
Description
Array_multisort () can be used to immediately sort several arrays or arrays of multiple sizes (multi-dimensional).
The input array is considered to be the column of the table (table), sorted by column (rows), similar to the function of SQL ORDER by clause, the first array is the primary (primary) sorted array, and the column (value) in this array is the same as an array of inputs below the same sort.
The argument structure of this function is a unique (unusual) bit, but it is flexible. The first argument must be an array, and subsequent arguments can be one of the array or the sort flag (flag) for the next list.
Sort order Flag:
SORT_ASC-Sort in ascending order
Sort_desc-Sort in descending order
Ordered type flag:
Sort_regular-Normal comparison items
Sort_numeric-Comparing items with numbers
Sort_string-as a string to compare items
You cannot use two flags of the same type to be specified after each array, the ordered flag is specified after the array argument, only works on the array, and the others are reset to the preset sort_asc and sort_regular after the array argument.
The success returns True, and the failure returns false.
function definitions and Syntax *************
Uasort ()
The function uses a user-defined comparison function to sort the array and keep the index associated (no new keys are assigned to the element).
Returns TRUE if successful, otherwise FALSE.
This function is primarily used to sort the associative arrays that are important for the cell order.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specifies 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, returns 0
If a < B, return 1
If a > B, return-1
PHP Uasort () function
Definition and usage
The Uasort () function uses a user-defined comparison function to sort the array and keep the index associated (no new keys are assigned to the element).
Returns TRUE if successful, otherwise FALSE.
This function is primarily used to sort the associative arrays that are important for the cell order.
Grammar
Uasort (array,sorttype) parameter description
Array required. Specifies 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, returns 0
If a < B, return 1
If a > B, return-1
Example
Copy CodeThe code is as follows:
function My_sort ($a, $b)
{
if ($a = = $b) return 0;
return ($a > $b)? -1:1;
}
$people = Array ("Swanson" = "Joe",
"Griffin" = "Peter", "Quagmire" and "Glenn",
"Swanson" = "Joe", "Griffin" and "Peter"
"Quagmire" = "Glenn");
Uasort ($people, "My_sort");
Print_r ($people);
?>

Output:
Copy CodeThe code is as follows:
Array
(
[Griffin] = Peter
[Swanson] = Joe
[Quagmire] = Glenn
[Griffin] = Peter
[Swanson] = Joe
[Quagmire] = Glenn
)

http://www.bkjia.com/PHPjc/323017.html www.bkjia.com true http://www.bkjia.com/PHPjc/323017.html techarticle Example: (Concise) Uasort ($arr, create_function (' $a, $b ', ' return $a [\ ' line_num\ '] $b [\ ' line_num\ ']; ')); function definitions and Syntax ************* array_multisort (PHP4 = 4.0b4) ar ...

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