Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

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

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☆
        )

)
shy_azusa
Ruby/JavaScript/CoffeeScript/PHP/iPhone 8/PS4/iMac/MBP/macOS/iOS/MtG/Carbonara/uncensored
temona
"てもなく"とは「簡単に、たやすく」という意味を持った古語です。 テモナの社名にはIT技術で世の中を便利にしたいという想いが込められています。
http://temona.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした