Home > Backend Development > PHP Tutorial > PHP array sorting The difference between array_multisort and uasort_PHP tutorial

PHP array sorting The difference between array_multisort and uasort_PHP tutorial

WBOY
Release: 2016-07-21 15:31:27
Original
881 people have browsed it

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 -- - Sorting arrays of composite or diverse sizes
Syntax: bool array_multisort (array ar1 [,mixed arg [,mixed ...[,array...]]])
Description:
Array_multisort( ) can Used to sort several arrays or multi-dimensional arrays at once.
The input array is regarded as a field of the table and is sorted according to the rows. This is similar to the function of the SQL ORDER BY clause. The first array is the primary sorted array. The columns (values) in this array are sorted in the same order as the next input array.
The argument structure of this function is a unique bit, but it is flexible. The first argument must be an array, and subsequent arguments can be an array or one of the sort flags of the next list.
Sort order flags:
SORT_ASC - Sort in ascending order
SORT_DESC - Sort in descending order
Sort type flags:
SORT_REGULAR - Normal comparison items
SORT_NUMERIC - Compare items as numbers
SORT_STRING - Compare items as strings
You cannot use two flags of the same type specified after each array. The sorting flag is specified after the array argument, only Acts on this array, others will be reset to the default SORT_ASC and SORT_REGULAR after the array argument.
Returns true if successful and false if failed.
******************Function definition and syntax****************
uasort()
The function uses a user-defined comparison function Sorts the array, maintaining index associativity (no assigning new keys to elements).
Returns TRUE if successful, otherwise returns FALSE.
This function is mainly used to sort associative arrays where the order of units is important.
Syntax
uasort(array,sorttype) Parameter Description
array Required. Specifies the array to be sorted.
function required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments for comparison, and work in a manner similar to the following:
If a = b, return 0
If a < b, returns 1
If a > b, returns -1
PHP uasort() function
Definition and usage
uasort() function uses user-defined comparison function to sort the array , and maintain index association (no new keys are assigned to elements).
Returns TRUE if successful, otherwise returns FALSE.
This function is mainly used to sort associative arrays where the order of units is important.
Syntax
uasort(array,sorttype) Parameter Description
array Required. Specifies the array to be sorted.
function required. User-defined functions.
The function must be designed to return -1, 0, or 1, and should accept two arguments 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 The 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" => " 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
)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323017.htmlTechArticleExample: (concise) uasort($arr,create_function('$a, $b','return $a ['line_num']$b['line_num'];')); *************Function definition and syntax**************** array_multisort (PHP4 = 4.0b4) ar...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template