The difference between array_multisort and uasort

  • 2020-03-31 21:39:49
  • OfStack

Example: (plain)
Uasort ($arr, create_function (' $a, $b ', 'return $a [\' line_num \] < $b [\ 'line_num \']; '));
************* * function definition and syntax ************* ******
array_multisort
PHP4 (> = 4.0 b4)
Array_multisort - sorts arrays of multiple or multiple sizes
Syntax: bool array_multisort (array ar1 [,mixed arg [,mixed...[,array...]])
Description:
Array_multisort() can be used to sort several arrays or multi-dimensional arrays at once.
The input array is treated as the column of the table and sorted BY column (rows), which is similar to the function of the SQL ORDER BY clause.
The argument structure of this function is a unique bit, but flexible. The first argument must be an array, and the subsequent argument may be an array or one of the sort flags of the next list.
Sorting order flag:
SORT_ASC - sort in ascending order
SORT_DESC - sort in descending order
Sorting type flag:
SORT_REGULAR - normal comparison items
SORT_NUMERIC - compares items to Numbers
SORT_STRING - think of it as a string to compare items
Two flags of the same type cannot be specified after each array, the sorted flags are specified after the array arguments, only for this array, the others will be reset to the preset SORT_ASC and SORT_REGULAR after the array arguments.
Returns true on success and false on failure.
************* * function definition and syntax ************* ******
Uasort ()
The array_sort () function sorts an array using a user-defined comparison function and keeps the index associated (no new keys are assigned to the elements).
Returns TRUE on success, or FALSE otherwise.
This function is primarily used to sort a combination array whose cell order is important.
grammar
Uasort (array,sorttype) parameter description
Array required. Specifies the array to sort.
The function required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments for comparison while working in a manner similar to the following:
If a = b, return 0
If a < B, returns 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 (without assigning new keys to the elements).
Returns TRUE on success, or FALSE otherwise.
This function is primarily used to sort a combination array whose cell order is important.
grammar
Uasort (array,sorttype) parameter description
Array required. Specifies the array to sort.
The function required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments for comparison while working in a manner similar to the following:
If a = b, return 0
If a < B, returns 1
If a > B, return 1
example
 
<?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:
 
Array 
( 
[griffin] => peter 
[swanson] => joe 
[quagmire] => glenn 
[Griffin] => Peter 
[Swanson] => Joe 
[Quagmire] => Glenn 
) 

Related articles: