Php array_multisort đa chiều

Hoặc chỉ kêu gọi

$property = 'count';
array_multisort[array_column[$array, $property], $array];
35 với người điều hành tàu vũ trụ để thực hiện ít lặp lại hơn trong trường hợp này. Thử nghiệm

usort[$array, fn[$a, $b] => $a->count  $b->count];

Lưu ý rằng mặc dù các giá trị đếm được chuyển thành các giá trị kiểu chuỗi trong mảng đầu vào, cả hai hàm sắp xếp sẽ sắp xếp chính xác các giá trị theo số thay vì sắp xếp theo thứ tự bảng chữ cái [đặt nhầm

$property = 'count';
array_multisort[array_column[$array, $property], $array];
36 trước `420]. Đây là một tính năng mặc định đáng tin cậy

Ngay cả khi bạn đang khai báo cột để sắp xếp theo nhiều cách khác nhau, thì cả hai cách tiếp cận đều cho phép sử dụng biến mà không cần bất kỳ kỹ thuật bổ sung nào

Multisort Demo với biến

$property = 'count';
array_multisort[array_column[$array, $property], $array];

Usort Demo với biến

$property = 'count';
usort[$array, fn[$a, $b] => $a->$property  $b->$property];

Cả hai hàm sắp xếp riêng đều sửa đổi theo tham chiếu, vì vậy đừng cố truy cập mảng đã sắp xếp theo giá trị trả về của chúng

Hướng sắp xếp mặc định của

$property = 'count';
array_multisort[array_column[$array, $property], $array];
37 là tăng dần, vì vậy sẽ không có ích lợi gì khi sử dụng rõ ràng
usort[$array, fn[$a, $b] => $a->count  $b->count];
0 giữa hai tham số mảng. Nếu muốn sắp xếp giảm dần, hãy viết
usort[$array, fn[$a, $b] => $a->count  $b->count];
1 giữa hai mảng [làm tham số thứ hai]

$property = 'count';
array_multisort[array_column[$array, $property], $array];
35 sẽ sắp xếp tăng dần khi thân hàm tùy chỉnh đặt dữ liệu
usort[$array, fn[$a, $b] => $a->count  $b->count];
3 ở bên trái của toán tử tàu vũ trụ và dữ liệu
usort[$array, fn[$a, $b] => $a->count  $b->count];
4 ở bên phải. Để sắp xếp theo chiều giảm dần, chỉ cần ghi dữ liệu
usort[$array, fn[$a, $b] => $a->count  $b->count];
4 bên trái và dữ liệu
usort[$array, fn[$a, $b] => $a->count  $b->count];
3 bên phải

Cả hai cách tiếp cận đều có khả năng nhận được nhiều quy tắc sắp xếp, nhưng vì câu hỏi này chỉ yêu cầu sắp xếp trên một cột nên hướng dẫn đó không phù hợp ở đây

Sẽ kém hiệu quả hơn khi gọi một hàm [như

usort[$array, fn[$a, $b] => $a->count  $b->count];
7] trên mỗi lần lặp trong khi sắp xếp. Đây không còn là phương pháp hay nhất nữa. Không sử dụng so sánh hai chiều [như
usort[$array, fn[$a, $b] => $a->count  $b->count];
8 hoặc
usort[$array, fn[$a, $b] => $a->count  $b->count];
9] để trả về kết quả boolean. Một so sánh ba chiều được mong đợi từ
$property = 'count';
array_multisort[array_column[$array, $property], $array];
35

Để sắp xếp dữ liệu với nhiều quy tắc/cột/thuộc tính, câu trả lời này đưa ra hướng dẫn tốt

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đưa ra một mảng các đối tượng và nhiệm vụ là sắp xếp mảng theo đối tượng theo các trường đã cho

    Cách tiếp cận.
    Hàm usort[] là một hàm có sẵn trong PHP dùng để sắp xếp mảng các phần tử theo điều kiện với một hàm so sánh cho trước. Hàm usort[] cũng có thể được sử dụng để sắp xếp một mảng các đối tượng theo trường đối tượng. Gọi hàm usort[] với đối số đầu tiên là mảng đối tượng và đối số thứ hai là hàm so sánh trên cơ sở so sánh giữa hai đối tượng mảng phải được thực hiện.

    Ví dụ

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    1

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    2
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    3
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    4
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    4
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    0
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    2
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    5
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    7
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    10
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1______312

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    14

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    4
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    0
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1401
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    5
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1406
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    10
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1371

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    14

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    4
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    0
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1350
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    5
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1355
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    10
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    1____1360

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    362

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    363

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    364
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    365

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    367
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    368
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    370
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    372
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    373

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    375
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    376
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    377
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    378
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    379

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    01____002
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    378
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    04
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    5
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    06

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    01______009
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    378
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    04
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    0
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    06

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    9
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    01____016
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    378
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    04
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    10
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    06

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    22

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    22

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    376
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    25____1378
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    379

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    29
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    3
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    31
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    377
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    378
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    363

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    36
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    29
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    373

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    22

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    376
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    41
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    42
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    44
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    379

    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    6
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    36
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    42
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    49
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    44
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    51

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    22

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    53
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    3
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    55
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    57
    $property = 'count';
    usort[$array, fn[$a, $b] => $a->$property  $b->$property];
    
    3
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    2
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    363

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    61
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    5____063
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    363

    _______065____053____1363

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    68______053____23
    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    71
    $property = 'count';
    array_multisort[array_column[$array, $property], $array];
    
    363

    _______061____15____075____1363

    _______065____053____1363

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    80

    đầu ra

    usort[$array, fn[$a, $b] => $a->count  $b->count];
    
    1

    Làm thế nào để bạn sắp xếp một mảng các đối tượng theo thuộc tính trong PHP?

    Cách tiếp cận. Hàm usort[] là một hàm có sẵn trong PHP, được sử dụng để sắp xếp mảng các phần tử theo điều kiện với một hàm so sánh đã cho. Hàm usort[] cũng có thể được sử dụng để sắp xếp một mảng các đối tượng theo trường đối tượng.

    Chúng ta có thể sắp xếp mảng các đối tượng không?

    Chúng tôi có thể sắp xếp một mảng các đối tượng trong JavaScript bằng cách sử dụng Mảng tích hợp sẵn của chúng tôi. phương thức sắp xếp[] , tuy nhiên, chúng ta phải chuyển một hàm tùy chỉnh cho phương thức sắp xếp tích hợp của mình. Hàm này là cần thiết để so sánh các thuộc tính dựa trên đó chúng ta muốn sắp xếp mảng đối tượng của mình.

    Làm thế nào để bạn sắp xếp một mảng các phần tử?

    Mảng có thể được sắp xếp theo thứ tự tăng dần bằng cách liên tục tìm phần tử tối thiểu [xét theo thứ tự tăng dần] từ phần chưa sắp xếp và đặt nó ở đầu

    Chủ Đề