PHP 8.6.0RC7 available for testing

The SortDirection enum

(PHP 8 >= 8.6.0)

Вступ

The SortDirection enum is used to specify the direction in which values are sorted.

Зауваження:

As of PHP 8.6.0, none of the functions bundled with PHP accept a SortDirection. For example, array_multisort() still expects SORT_ASC or SORT_DESC.

Кородкий огляд переліку

enum SortDirection
{
case Ascending
case Descending
}

Приклади

Приклад #1 Sorting names with SortDirection

<?php

function sortNames(array $names, SortDirection $direction): array
{
    match ($direction) {
        SortDirection::Ascending => sort($names),
        SortDirection::Descending => rsort($names),
    };

    return $names;
}

$names = ['Charlie', 'Alice', 'Bob'];

print_r(sortNames($names, SortDirection::Descending));

Поданий вище приклад виведе:

Array
(
    [0] => Charlie
    [1] => Bob
    [2] => Alice
)

Прогляньте також

+add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top