PHPの多次元連想配列のソート

  • 61
    いいね
  • 2
    コメント

ただの配列であれば簡単にソートは出来てしまいますが、それが多次元であったときのソート方法です。

こんな配列があったと仮定します。

multisort.php
$array = array(
    array(
        'id'        => 0,
        'track_num' => 7,
        'title'     => 'ベィスドロップ・フリークス',
        'artist'    => 'かめりあ feat. ななひら'
    ),
    array(
        'id'        => 1,
        'track_num' => 1,
        'title'     => 'CHRONO DIVER -NORNIR-',
        'artist'    =>  'L.E.D.'
    ),  
    array(
        'id'        => 2,
        'track_num' => 3,
        'title'     => 'illuminate',
        'artist'    => 'Sota Fujimori'
    ),  
    array(
        'id'        => 3,
        'track_num' => 10, 
        'title'     => '表裏一体!?怪盗いいんちょの悩み♥',
        'artist'    => '◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆'
    ),  
    array(
        'id'        => 4,
        'track_num' => 28, 
        'title'     => 'Night sky',
        'artist'    => 'USAO'
    ),  
    array(
        'id'        => 5,
        'track_num' => 14, 
        'title'     => 'Night sky (Extended Mix)',
        'artist'    => 'USAO'
    ),  
    array(
        'id'        => 6,
        'track_num' => 30, 
        'title'     => 'X↑X↓',
        'artist'    => 'DJ TECHNORCH'
    ),  
    array(
        'id'        => 7,
        'track_num' => 11, 
        'title'     => 'X↑X↓ (Titanium Sisters Long Version)',
        'artist'    => 'DJ TECHNORCH'
    ),  
    array(
        'id'        => 8,
        'track_num' => 31, 
        'title'     => 'Line 4 Ruin',
        'artist'    => 'Ryu☆'
    ),
);

これを、track_num(トラックナンバー)でソートしたい時。
ソート用の配列を用意して array_multisort() で一撃。

foreach ((array) $array as $key => $value) {
    $sort[$key] = $value['track_num'];
}

array_multisort($sort, SORT_ASC, $array);
print_r($array);

実行結果

$ php multisort.php
Array
(
    [0] => Array
        (
            [id] => 1
            [track_num] => 1
            [title] => CHRONO DIVER -NORNIR-
            [artist] => L.E.D.
        )

    [1] => Array
        (
            [id] => 2
            [track_num] => 3
            [title] => illuminate
            [artist] => Sota Fujimori
        )

    [2] => Array
        (
            [id] => 0
            [track_num] => 7
            [title] => ベィスドロップ・フリークス
            [artist] => かめりあ feat. ななひら
        )

    [3] => Array
        (
            [id] => 3
            [track_num] => 10
            [title] => 表裏一体!?怪盗いいんちょの悩み♥
            [artist] => ◇◆噂の怪盗少女 ぷらずま★彡Prim◇◆
        )

    [4] => Array
        (
            [id] => 7
            [track_num] => 11
            [title] => X↑X↓ (Titanium Sisters Long Version)
            [artist] => DJ TECHNORCH
        )

    [5] => Array
        (
            [id] => 5
            [track_num] => 14
            [title] => Night sky (Extended Mix)
            [artist] => USAO
        )

    [6] => Array
        (
            [id] => 4
            [track_num] => 28
            [title] => Night sky
            [artist] => USAO
        )

    [7] => Array
        (
            [id] => 6
            [track_num] => 30
            [title] => X↑X↓
            [artist] => DJ TECHNORCH
        )

    [8] => Array
        (
            [id] => 8
            [track_num] => 31
            [title] => Line 4 Ruin
            [artist] => Ryu☆
        )

)