BLOG

[PHP]配列のソート

| HTML/CSS/PHP

PHPで配列のソートをちょいちょい使うのでメモをします。
最近はサーバーサイドのJSとか使って色々しちゃうんでしょうけど、こういうのもね。
今回は手順とかいらんかなって思って書き方だけどん!

連想配列のソート

値でソートする

// 連想配列を配列に格納
$members = array(
  '01'=>'NAYEON',
  '02'=>'JEONGYEON',
  '03'=>'MOMO',
  '04'=>'SANA',
  '05'=>'JIHYO',
  '06'=>'MINA',
  '07'=>'DAHYUN',
  '08'=>'CHAEYOUNG',
  '09'=>'TZUYU'
);


// asortで値を昇順でソート
asort($members);

print_r($members);
// 結果
Array
(
    [08] => CHAEYOUNG
    [07] => DAHYUN
    [02] => JEONGYEON
    [05] => JIHYO
    [06] => MINA
    [03] => MOMO
    [01] => NAYEON
    [04] => SANA
    [09] => TZUYU
)


// arsortで値を降順でソート
arsort($members);

print_r($members);
// 結果
Array
(
    [09] => TZUYU
    [04] => SANA
    [01] => NAYEON
    [03] => MOMO
    [06] => MINA
    [05] => JIHYO
    [02] => JEONGYEON
    [07] => DAHYUN
    [08] => CHAEYOUNG
)

キーでソートする

// 連想配列を配列に格納
$members = array(
  '01'=>'NAYEON',
  '02'=>'JEONGYEON',
  '03'=>'MOMO',
  '04'=>'SANA',
  '05'=>'JIHYO',
  '06'=>'MINA',
  '07'=>'DAHYUN',
  '08'=>'CHAEYOUNG',
  '09'=>'TZUYU'
);

// ksortでキーを昇順でソート
ksort($members);

print_r($members);
// 結果
Array
(
    [01] => NAYEON
    [02] => JEONGYEON
    [03] => MOMO
    [04] => SANA
    [05] => JIHYO
    [06] => MINA
    [07] => DAHYUN
    [08] => CHAEYOUNG
    [09] => TZUYU
)

// krsortでキーを降順でソート
krsort($members);

print_r($members);
// 結果
Array
(
    [09] => TZUYU
    [08] => CHAEYOUNG
    [07] => DAHYUN
    [06] => MINA
    [05] => JIHYO
    [04] => SANA
    [03] => MOMO
    [02] => JEONGYEON
    [01] => NAYEON
)



多次元連想配列のソート

// 多次元連想配列を変数に格納
$members = array(
  array('id'=>2,'name'=>'JEONGYEON'),
  array('id'=>1,'name'=>'NAYEON'),
  array('id'=>9,'name'=>'TZUYU'),
  array('id'=>5,'name'=>'JIHYO'),
  array('id'=>6,'name'=>'MINA'),
  array('id'=>3,'name'=>'MOMO'),
  array('id'=>7,'name'=>'DAHYUN'),
  array('id'=>8,'name'=>'CHAEYOUNG'),
  array('id'=>4,'name'=>'SANA'),
);

// foreachで1つずつ値を取り出す
foreach ($members as $key => $value) {
  $id[$key] = $value['id'];
}

// array_multisortで'id'の列を昇順に並び替える
array_multisort($id, SORT_ASC, $members);

// ソート済みの変数を出力
print_r($members);

// 結果

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => NAYEON
        )

    [1] => Array
        (
            [id] => 2
            [name] => JEONGYEON
        )

    [2] => Array
        (
            [id] => 3
            [name] => MOMO
        )

    [3] => Array
        (
            [id] => 4
            [name] => SANA
        )

    [4] => Array
        (
            [id] => 5
            [name] => JIHYO
        )

    [5] => Array
        (
            [id] => 6
            [name] => MINA
        )

    [6] => Array
        (
            [id] => 7
            [name] => DAHYUN
        )

    [7] => Array
        (
            [id] => 8
            [name] => CHAEYOUNG
        )

    [8] => Array
        (
            [id] => 9
            [name] => TZUYU
        )

)

とりあえず、僕がよく使う配列のソートでよく使うのは「多次元連想配列」でのソートです。
これはもうちょっと簡単にできたらいいのにね。
知ってる方は教えて下さいw

PAGE TOP