-1

重複の可能性:
PHP での多次元配列のソート

多次元配列のキーでソートするにはどうすればよいですか?

たとえば、以下はデータベースから印刷した配列で、最新のものから順に - 12 月、11 月、10 月など2011 年、2010 年、2009 年などです。

Array
(
    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [3] => Array
        (
            [URL] => november 2011
            [Title] => November 2011
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )

    [4] => Array
        (
            [URL] => april 2011
            [Title] => April 2011
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

)

しかし、 10 月、11 月、12 月など2011 年、2010 年、2009 年などのようにする必要があります

したがって、配列は次のようにソートする必要があります。

Array
(

    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )

    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )

    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )

    [4] => Array
        (
            [URL] => april 2010
            [Title] => April 2010
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )

    [3] => Array
        (
            [URL] => november 2010
            [Title] => November 2010
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )
)

それは可能ですか?

4

3 に答える 3

3

複数のキーを持つ配列の配列を並べ替える一般的なソリューション

この質問に対する私の答えに基づいて、これは多くの状況で使用できる非常に一般的なソリューションです。

制限:無名関数が存在するため、PHP>=5.3が機能する必要があります。

新しく改良され、降順の並べ替えがサポートされるようになりました

function make_comparer() {
    $criteriaNames = func_get_args();
    $comparer = function($first, $second) use ($criteriaNames) {
        // Do we have anything to compare?
        while(!empty($criteriaNames)) {
            // What will we compare now?
            $criterion = array_shift($criteriaNames);

            // Used to reverse the sort order by multiplying
            // 1 = ascending, -1 = descending
            $sortOrder = 1; 
            if (is_array($criterion)) {
                $sortOrder = $criterion[1] == SORT_DESC ? -1 : 1;
                $criterion = $criterion[0];
            }

            // Do the actual comparison
            if ($first[$criterion] < $second[$criterion]) {
                return -1 * $sortOrder;
            }
            else if ($first[$criterion] > $second[$criterion]) {
                return 1 * $sortOrder;
            }

        }

        // Nothing more to compare with, so $first == $second
        return 0;
    };

    return $comparer;
}

それの使い方

年の昇順で並べ替えるには:

uasort($array, make_comparer('Year'));

年の昇順、次に月の昇順で並べ替えるには、次のようにします。

uasort($array, make_comparer('Year', 'Month'));

年の降順、次に月の昇順で並べ替えるには、次のようにします。

uasort($array, make_comparer(array('Year', SORT_DESC), 'Month'));

この最後のものはあなたが求めているものです。

于 2011-09-16T11:21:56.723 に答える
0

Assuming that the data you would have provided in your question would have been actually correct (taking Year, Month, Date as base to create a sort on), you can first index the array by those values and the sort the main array. I'm just writing it, otherwise you might misread the output of the Demo, the URL/Titles do not correspond to the numerical values given, but it just works:

// create an index by date
foreach($data as $k=>$v)
{
    $index[$k] = sprintf('%04d-%02d-%02d', $v['Year'], $v['Month'], $v['Date']);
}

// sort data based on the index
array_multisort($index, SORT_DESC, $data);
于 2011-09-16T11:28:49.723 に答える
0

どうですか:

データ:

$arr = Array (
    Array (
        'URL' => 'september 2011',
        'Title' => 'September 2011',
        'Date' => 8,
        'Month' => 9,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'january 2011',
        'Title' => 'January 2011',
        'Date' => 1,
        'Month' => 2,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'february 2011',
        'Title' => 'February 2011',
        'Date' => 4,
        'Month' => 1,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'november 2011',
        'Title' => 'November 2011',
        'Date' => 23,
        'Month' => 11,
        'Year' => 2010,
    ),
    Array (
        'URL' => 'april 2011',
        'Title' => 'April 2011',
        'Date' => 23,
        'Month' => 4,
        'Year' => 2010,
    ),
);

コード:

function compare($a, $b) {
    if ($a['Year'] == $b['Year']) {
      return ($a['Month'] - $b['Month']);
    } else {
        return ($b['Year'] - $a['Year']);
    }
}    
usort($arr, 'compare');
print_r($arr);    

出力:

Array
(
[0] => Array
    (
        [URL] => february 2011
        [Title] => February 2011
        [Date] => 4
        [Month] => 1
        [Year] => 2011
    )

[1] => Array
    (
        [URL] => january 2011
        [Title] => January 2011
        [Date] => 1
        [Month] => 2
        [Year] => 2011
    )

[2] => Array
    (
        [URL] => september 2011
        [Title] => September 2011
        [Date] => 8
        [Month] => 9
        [Year] => 2011
    )

[3] => Array
    (
        [URL] => april 2011
        [Title] => April 2011
        [Date] => 23
        [Month] => 4
        [Year] => 2010
    )

[4] => Array
    (
        [URL] => november 2011
        [Title] => November 2011
        [Date] => 23
        [Month] => 11
        [Year] => 2010
    )

)
于 2011-09-16T11:44:42.487 に答える