Hướng dẫn
Quảng cáo

Hàm array_multisort() trong PHP

Hướng dẫn cách sử dụng hàm array_multisort() về mảng trong lập trình PHP

Tác dụng của hàm array_multisort()

The array_multisort() function sort multiple arrays at once, or a multi-dimensional array by one or more dimensions. The sorting is done as though the arrays were columns in a table.

Associative or string keys will be preserved, but numeric keys will be re-indexed.

The following table summarizes the technical details of this function.

Return Value: Returns TRUE on success or FALSE on failure.
Changelog:

The sort flags SORT_NATURAL and SORT_FLAG_CASE were added in PHP 5.4.0

The sort flag SORT_LOCALE_STRING was added in PHP 5.3.0
Version: PHP 4+

Syntax

The basic syntax of the array_multisort() function is given with:

array_multisort(array1, array1_sort_order, array1_sort_flags, ...)

The following example shows the array_multisort() function in action.

<?php
// Sample arrays
$array1 = array(2, 7, 10, 5);
$array2 = array(4, 3, 1, 2);

// Sorting multiple arrays
array_multisort($array1, $array2);
print_r($array1);
print_r($array2);
?>

Tip: The first array is the main one to sort by; all items in the other arrays are reordered based on the sorted order of the first array (i.e., arrays are treated as columns of a table). If items in the first array compare as equal, the sort order is determined by the second array, and so on.

In the above example after sorting, the first array will contain 2, 5, 7, 10, the second array will contain 4, 2, 3, 1. Notice that the entries in the second array corresponds to the entries in the first array. To better understand this, let's take a closer look at the following illustration.

     array1 | array2
    --------+--------
        2   |      4
        7   |      3
        10  |      1
        5   |      2
 
 array1 | array2
--------+--------
    2   |      4
    5   |      2
    7   |      3
    10  |      1
     Before Sorting    After Sorting

Parameters

The array_multisort() function accepts the following parameters.

Parameter Description
array1 Required. Specifies the array to sort.
array1_sort_order

Optional. Specifies the sorting order. Possible values are:

  • SORT_ASC – Sort in ascending order (A-Z). Default value.
  • SORT_DESC – Sort in descending order (Z-A).
array1_sort_flags

Optional. Specifies how array items should be compared. Possible values are:

  • SORT_REGULAR – Compare items normally (don't change types). Default value.
  • SORT_NUMERIC – Compare items numerically.
  • SORT_STRING – Compare items as strings.
  • SORT_LOCALE_STRING – Compare items as strings, based on the current locale.
  • SORT_NATURAL – Compare items as strings using "natural ordering" algorithm, like the PHP natsort() function.
  • SORT_FLAG_CASE – Can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively.
... Optional. Specifies more arrays to sort, optionally followed by sort order and flags. Only elements corresponding to equivalent elements in previous arrays are compared.

More Examples

Here're some more examples showing how array_multisort() function basically works:

The following example shows how to sort a multi-dimensional array with this function.

<?php
// Sample array
$array = array(
    array("10", 11, 100, 100, "a"),
    array(1, 4, "2", 5, 3)
);

// Sorting multi-dimensional array
array_multisort($array[0], SORT_STRING, $array[1], SORT_DESC, SORT_NUMERIC);
print_r($array);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements