1. 程式人生 > >array_multisort 二維陣列排序

array_multisort 二維陣列排序

用PHP自帶array_multisort函式排序 
<?php

    $data = array();
    $data[] = array('volume' => 67, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 1);
    $data[] = array('volume' => 85, 'edition' => 6);
    $data[] = array('volume' => 98, 'edition' => 2);
    $data[] = array('volume' => 86, 'edition' => 6);
    $data[] = array('volume' => 67, 'edition' => 7);

    // 取得列的列表
    foreach ($data as $key => $row)
    {
        $volume[$key]  = $row['volume'];
        $edition[$key] = $row['edition'];
    }

    array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

    print_r($data);
?>

輸出結果:

複製程式碼 程式碼如下: Array
(
    [0] => Array
        (
            [volume] => 98
            [edition] => 2
        )
    [1] => Array
        (
            [volume] => 86
            [edition] => 1
        )
    [2] => Array
        (
            [volume] => 86
            [edition] => 6
        )
    [3] => Array
        (
            [volume] => 85
            [edition] => 6
        )
    [4] => Array
        (
            [volume] => 67
            [edition] => 2
        )
    [5] => Array
        (
            [volume] => 67
            [edition] => 7
        )
)