Hướng dẫn php sort array by two keys - php sắp xếp mảng theo hai phím

PHP7 làm cho việc sắp xếp theo nhiều cột siêu dễ dàng với toán tử tàu vũ trụ (<=>) hay còn gọi là "toán tử so sánh kết hợp" hoặc "toán tử so sánh ba chiều".

Tài nguyên: https://wiki.php.net/rfc/combined-comparison-operator

Sắp xếp theo nhiều cột cũng đơn giản như việc viết các mảng cân bằng/quan hệ ở cả hai phía của toán tử. Dễ dàng thực hiện!

Khi giá trị $a ở bên trái của toán tử tàu vũ trụ và giá trị $b ở bên phải, việc sắp xếp tăng dần được sử dụng.

Khi giá trị $b ở bên trái của toán tử tàu vũ trụ và giá trị $a ở bên phải, việc sắp xếp giảm dần được sử dụng.

Khi toán tử tàu vũ trụ so sánh hai chuỗi số, nó sẽ so sánh chúng là số - do đó bạn sẽ tự động sắp xếp tự động.

Tôi đã không sử dụng

array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
2 vì tôi không thấy bất kỳ nhu cầu bảo tồn các chỉ mục ban đầu.

Mã: (demo) - Sắp xếp theo

array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
3 ASC, sau đó
array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
4 ASC, sau đó
array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
5 ASC

$array = [
    ['ID' => 1, 'title' => 'Boring Meeting', 'date_start' => '2010-07-30', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 2, 'title' => 'Find My Stapler', 'date_start' => '2010-07-22', 'event_type' => 'meeting', 'state' => 'new-york'],
    ['ID' => 3, 'title' => 'Mario Party', 'date_start' => '2010-07-22', 'event_type' => 'party', 'state' => 'new-york'],
    ['ID' => 4, 'title' => 'Duct Tape Party', 'date_start' => '2010-07-28', 'event_type' => 'party', 'state' => 'california']
];

usort($array, function($a, $b) {
    return [$a['state'], $a['event_type'], $a['date_start']]
           <=>
           [$b['state'], $b['event_type'], $b['date_start']];
});

var_export($array);

Đầu ra

array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)

P.S. Cú pháp mũi tên với PHP7.4 trở lên (demo) ...

usort($array, fn($a, $b) =>
    [$a['state'], $a['event_type'], $a['date_start']]
    <=>
    [$b['state'], $b['event_type'], $b['date_start']]
);

Kỹ thuật tương đương với

array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
6 và cuộc gọi
array (
  0 => 
  array (
    'ID' => 4,
    'title' => 'Duct Tape Party',
    'date_start' => '2010-07-28',
    'event_type' => 'party',
    'state' => 'california',
  ),
  1 => 
  array (
    'ID' => 2,
    'title' => 'Find My Stapler',
    'date_start' => '2010-07-22',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  2 => 
  array (
    'ID' => 1,
    'title' => 'Boring Meeting',
    'date_start' => '2010-07-30',
    'event_type' => 'meeting',
    'state' => 'new-york',
  ),
  3 => 
  array (
    'ID' => 3,
    'title' => 'Mario Party',
    'date_start' => '2010-07-22',
    'event_type' => 'party',
    'state' => 'new-york',
  ),
)
7 cho mọi tiêu chí sắp xếp là: (demo)

array_multisort(
    array_column($array, 'state'),
    array_column($array, 'event_type'),
    array_column($array, 'date_start'),
    $array
);

Hàm này được sử dụng để sắp xếp mảng dựa trên một số cột.

Nhiều khoảnh khắc đã đến nơi tôi có một mảng PHP từ cơ sở dữ liệu và tôi phải sắp xếp nó dựa trên một số khóa mảng.

Mảng PHP chưa phân loại

Cú pháp:

 '',
        'UnreadCount' => '0',
        'RefrenceType' => 'Group',
        'DisplayName' => 'John and 3 others.',
        'ModifiedDate' => '2014-09-26 06:57:46',
        'EntryDate' => '2014-09-26 10:04:45'
    ),
    array(
        'ProfilePic' => '',
        'UnreadCount' => '3',
        'RefrenceType' => 'Group',
        'DisplayName' => 'Alex and 2 others.',
        'ModifiedDate' => '2014-09-26 10:05:38',
        'EntryDate' => NULL
    ),
    array(
        'UnreadCount' => '2',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/1c33a3a47d7ff48fbbced5bb6ea3face',
        'DisplayName' => 'Riya',
        'EntryDate' => '2014-09-28 10:14:27',
        'ModifiedDate' => '2014-09-28 12:47:53'
    ),
    array(
        'UnreadCount' => '4',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/1b01b7925d3f7dd230c166ee537b7f85',
        'DisplayName' => 'Vandana',
        'EntryDate' => '2014-09-26 09:56:11',
        'ModifiedDate' => '2014-09-22 13:12:54'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/64bb6ac3074e63791d6291b72bd5e42b',
        'DisplayName' => 'Celly',
        'EntryDate' => '2014-09-26 09:54:53',
        'ModifiedDate' => '2014-09-22 12:52:22'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http => //www.example.com/GetUserImage/995054e37437feb61d4d2b0b84cb71c6',
        'DisplayName' => 'Vijay',
        'EntryDate' => '2014-09-26 07:30:42',
        'ModifiedDate' => '2014-09-22 12:50:40'
    ),
    array(
        'UnreadCount' => '0',
        'RefrenceType' => 'User',
        'ProfilePic' => 'http://www.example.com/GetUserImage/27c556a98b25953c1df96d01d7262c00',
        'DisplayName' => 'Vikas M.',
        'EntryDate' => '2014-09-26 18:08:51',
        'ModifiedDate' => '2014-09-22 13:21:05'
    )
);
?>

Chức năng sắp xếp mảng PHP

Cú pháp:

 0 && !empty($SortBy)) {
            $Map = array();                     
            foreach ($Array as $Key => $Val) {
                $Sort_key = '';                         
                                foreach ($SortBy as $Key_key) {
                                        $Sort_key .= $Val[$Key_key];
                                }                
                $Map[$Key] = $Sort_key;
            }
            asort($Map, $Sort);
            $Sorted = array();
            foreach ($Map as $Key => $Val) {
                $Sorted[] = $Array[$Key];
            }
            return array_reverse($Sorted);
    }
    return $Array;
}

$Array = phparraysort($Array, array('UnreadCount','EntryDate','ModifiedDate'));
print_r($Array);
?>

Chức năng sắp xếp mảng PHP

Trong ví dụ này, tôi đã sắp xếp một mảng bằng nhiều tên cột không đọc, EntryDate và ModifiedDate.

Sắp xếp mảng PHP

Array
(
    [0] => Array
        (
            [UnreadCount] => 4
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/1b01b7925d3f7dd230c166ee537b7f85
            [DisplayName] => Vandana
            [EntryDate] => 2014-09-26 09:56:11
            [ModifiedDate] => 2014-09-22 13:12:54
        )

    [1] => Array
        (
            [ProfilePic] => 
            [UnreadCount] => 3
            [RefrenceType] => Group
            [DisplayName] => Alex and 2 others.
            [ModifiedDate] => 2014-09-26 10:05:38
            [EntryDate] => 
        )

    [2] => Array
        (
            [UnreadCount] => 2
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/1c33a3a47d7ff48fbbced5bb6ea3face
            [DisplayName] => Riya
            [EntryDate] => 2014-09-28 10:14:27
            [ModifiedDate] => 2014-09-28 12:47:53
        )

    [3] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http://www.example.com/GetUserImage/27c556a98b25953c1df96d01d7262c00
            [DisplayName] => Vikas M.
            [EntryDate] => 2014-09-26 18:08:51
            [ModifiedDate] => 2014-09-22 13:21:05
        )

    [4] => Array
        (
            [ProfilePic] => 
            [UnreadCount] => 0
            [RefrenceType] => Group
            [DisplayName] => John and 3 others.
            [ModifiedDate] => 2014-09-26 06:57:46
            [EntryDate] => 2014-09-26 10:04:45
        )

    [5] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/64bb6ac3074e63791d6291b72bd5e42b
            [DisplayName] => Celly
            [EntryDate] => 2014-09-26 09:54:53
            [ModifiedDate] => 2014-09-22 12:52:22
        )

    [6] => Array
        (
            [UnreadCount] => 0
            [RefrenceType] => User
            [ProfilePic] => http => //www.example.com/GetUserImage/995054e37437feb61d4d2b0b84cb71c6
            [DisplayName] => Vijay
            [EntryDate] => 2014-09-26 07:30:42
            [ModifiedDate] => 2014-09-22 12:50:40
        )

)

Hướng dẫn php sort array by two keys - php sắp xếp mảng theo hai phím

Làm cách nào để sắp xếp một mảng trong nhiều cột trong PHP?

Ví dụ #1 Sắp xếp nhiều mảng $ ar1 = mảng (10, 100, 100, 0); $ ar2 = mảng (1, 3, 2, 4); Array_Multisort ($ AR1, $ AR2);array_multisort($ar1, $ar2);

Làm cách nào để sắp xếp một mảng đa chiều trong PHP?

Hàm mảng_Multisort () trả về một mảng được sắp xếp.Bạn có thể gán một hoặc nhiều mảng.Hàm sắp xếp mảng đầu tiên và các mảng khác theo sau, sau đó, nếu hai hoặc nhiều giá trị giống nhau, nó sẽ sắp xếp mảng tiếp theo, v.v.array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

Usort hoạt động như thế nào trong PHP?

Hàm usort () trong PHP sắp xếp một mảng nhất định bằng cách sử dụng hàm so sánh do người dùng xác định.Hàm này rất hữu ích trong trường hợp nếu chúng ta muốn sắp xếp mảng theo cách mới.Hàm này gán các khóa tích phân mới bắt đầu từ 0 đến các phần tử có trong mảng và các khóa cũ bị mất.sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.

KSORT PHP là gì?

Hàm ksort () sắp xếp một mảng kết hợp theo thứ tự tăng dần, theo khóa.Mẹo: Sử dụng hàm krsort () để sắp xếp một mảng kết hợp theo thứ tự giảm dần, theo khóa.Mẹo: Sử dụng hàm orort () để sắp xếp một mảng kết hợp theo thứ tự tăng dần, theo giá trị.sorts an associative array in ascending order, according to the key. Tip: Use the krsort() function to sort an associative array in descending order, according to the key. Tip: Use the asort() function to sort an associative array in ascending order, according to the value.