PHPのarray_multisort()
関数は、複数の配列や、多次元配列を、関連付けて並べ替える組み込み関数。
キーと値の相関関係を維持したまま、ソートする。
定義
論理型 array_multisort( 配列型 &$array[, 多様な型 $argA[, 多様な型 $argB[, 多様な型 $...]]] );
パラメータ
- 配列型 &$array
並べ替える対象の配列を指定。
- 多様な型 $argA
昇順か降順かを指定する。
「SORT_ASC」を指定すると、昇順。
「SORT_DESC」を指定すると、降順。
- 多様な型 $argB
オプション。
並べ替える際の、型の取り扱いに関するフラグ。
- SORT_REGULAR: 型変換をしない、通常の比較。
- SORT_NUMERIC: 数値として比較。
- SORT_STRING: 文字列として比較。
- 多様な型 $…
追加の引数。
戻り値
論理値。
並べ替えに成功すると「TRUE」、失敗すると「FALSE」を返す。
構文
複数の配列を、関連付けて並べ替え
論理値 = array_multisort( 配列A, 配列B, 配列C );
まず、「配列A」の配列要素を基準に並べ替え、それでも同順の要素は、「配列B」を基準にし並べ、それでも同順の要素は、「配列C」を基準にし並べる。
「配列A」「配列B」「配列C」は、互いに関連づいている。
「配列A」「配列B」「配列C」の配列要素数は、同じでなければならない。
サンプル
2つの配列を、関連付けて並べ替え
<pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, $varArrayB );
print_r( $varArrayA );
print_r( $varArrayB );
?>
</pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, $varArrayB );
print_r( $varArrayA );
print_r( $varArrayB );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
3つの配列を、関連付けて並べ替え(1)
<pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array( "赤", "白", "緑", "青", "黒" );
$varArrayC = array( "Red", "White", "Green", "Blue", "Black" );
array_multisort( $varArrayA, $varArrayB, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array( "赤", "白", "緑", "青", "黒" );
$varArrayC = array( "Red", "White", "Green", "Blue", "Black" );
array_multisort( $varArrayA, $varArrayB, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
Array
(
[0] => White
[1] => Black
[2] => Red
[3] => Blue
[4] => Green
)
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
Array
(
[0] => White
[1] => Black
[2] => Red
[3] => Blue
[4] => Green
)
3つの配列を、関連付けて並べ替え(2)
<pre>
<?php
$varArrayA = array( 2, 1, 2, 2, 1 );
$varArrayB = array( 1, 1, 3, 2, 2 );
$varArrayC = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, $varArrayB, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
<?php
$varArrayA = array( 2, 1, 2, 2, 1 );
$varArrayB = array( 1, 1, 3, 2, 2 );
$varArrayC = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, $varArrayB, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
)
Array
(
[0] => 白
[1] => 黒
[2] => 赤
[3] => 青
[4] => 緑
)
3つの配列を、関連付けて並べ替え(昇順・降順を指定)
<pre>
<?php
$varArrayA = array( 2, 1, 2, 2, 1 );
$varArrayB = array( 1, 1, 3, 2, 2 );
$varArrayC = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, SORT_ASC, $varArrayB, SORT_DESC, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
<?php
$varArrayA = array( 2, 1, 2, 2, 1 );
$varArrayB = array( 1, 1, 3, 2, 2 );
$varArrayC = array( "赤", "白", "緑", "青", "黒" );
array_multisort( $varArrayA, SORT_ASC, $varArrayB, SORT_DESC, $varArrayC );
print_r( $varArrayA );
print_r( $varArrayB );
print_r( $varArrayC );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
Array
(
[0] => 2
[1] => 1
[2] => 3
[3] => 2
[4] => 1
)
Array
(
[0] => 黒
[1] => 白
[2] => 緑
[3] => 青
[4] => 赤
)
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
Array
(
[0] => 2
[1] => 1
[2] => 3
[3] => 2
[4] => 1
)
Array
(
[0] => 黒
[1] => 白
[2] => 緑
[3] => 青
[4] => 赤
)
配列と連想配列を、関連付けて並べ替え
<pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array(
"red" => "赤",
"white" => "白",
"green" => "緑",
"blue" => "青",
"black" => "黒"
);
array_multisort( $varArrayA, $varArrayB );
print_r( $varArrayA );
print_r( $varArrayB );
?>
</pre>
<?php
$varArrayA = array( 3, 1, 5, 4, 2 );
$varArrayB = array(
"red" => "赤",
"white" => "白",
"green" => "緑",
"blue" => "青",
"black" => "黒"
);
array_multisort( $varArrayA, $varArrayB );
print_r( $varArrayA );
print_r( $varArrayB );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[white] => 白
[black] => 黒
[red] => 赤
[blue] => 青
[green] => 緑
)
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
[white] => 白
[black] => 黒
[red] => 赤
[blue] => 青
[green] => 緑
)
キーと値の相関関係を維持したままである点に注目。
多次元配列を、関連付けて並べ替え
<pre>
<?php
$varArray = array(
array( 2, 1, 2, 2, 1 ),
array( 1, 1, 3, 2, 2 ),
array(
"red" => "赤",
"white" => "白",
"green" => "緑",
"blue" => "青",
"black" => "黒"
)
);
array_multisort( $varArray[0], $varArray[1], $varArray[2] );
print_r( $varArray );
?>
</pre>
<?php
$varArray = array(
array( 2, 1, 2, 2, 1 ),
array( 1, 1, 3, 2, 2 ),
array(
"red" => "赤",
"white" => "白",
"green" => "緑",
"blue" => "青",
"black" => "黒"
)
);
array_multisort( $varArray[0], $varArray[1], $varArray[2] );
print_r( $varArray );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
)
[2] => Array
(
[white] => 白
[black] => 黒
[red] => 赤
[blue] => 青
[green] => 緑
)
)
(
[0] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 2
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 1
[3] => 2
[4] => 3
)
[2] => Array
(
[white] => 白
[black] => 黒
[red] => 赤
[blue] => 青
[green] => 緑
)
)
データベースから取得したような、連想配列を並べ替え
<pre>
<?php
$data[] = array( 'name' => 'Tarou', 'mathematics' => 63, 'science' => 82);
$data[] = array( 'name' => 'Ichirou', 'mathematics' => 86, 'science' => 91);
$data[] = array( 'name' => 'Sakura', 'mathematics' => 75, 'science' => 76);
$data[] = array( 'name' => 'Keita', 'mathematics' => 98, 'science' => 55);
$data[] = array( 'name' => 'Tomoko', 'mathematics' => 75, 'science' => 82);
foreach( $data as $key => $row ) {
$name[$key] = $row['name'];
$mathematics[$key] = $row['mathematics'];
$science[$key] = $row['science'];
}
array_multisort( $mathematics, SORT_DESC, $science, SORT_DESC, $data );
print_r( $data );
?>
</pre>
<?php
$data[] = array( 'name' => 'Tarou', 'mathematics' => 63, 'science' => 82);
$data[] = array( 'name' => 'Ichirou', 'mathematics' => 86, 'science' => 91);
$data[] = array( 'name' => 'Sakura', 'mathematics' => 75, 'science' => 76);
$data[] = array( 'name' => 'Keita', 'mathematics' => 98, 'science' => 55);
$data[] = array( 'name' => 'Tomoko', 'mathematics' => 75, 'science' => 82);
foreach( $data as $key => $row ) {
$name[$key] = $row['name'];
$mathematics[$key] = $row['mathematics'];
$science[$key] = $row['science'];
}
array_multisort( $mathematics, SORT_DESC, $science, SORT_DESC, $data );
print_r( $data );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => Array
(
[name] => Keita
[mathematics] => 98
[science] => 55
)
[1] => Array
(
[name] => Ichirou
[mathematics] => 86
[science] => 91
)
[2] => Array
(
[name] => Tomoko
[mathematics] => 75
[science] => 82
)
[3] => Array
(
[name] => Sakura
[mathematics] => 75
[science] => 76
)
[4] => Array
(
[name] => Tarou
[mathematics] => 63
[science] => 82
)
)
(
[0] => Array
(
[name] => Keita
[mathematics] => 98
[science] => 55
)
[1] => Array
(
[name] => Ichirou
[mathematics] => 86
[science] => 91
)
[2] => Array
(
[name] => Tomoko
[mathematics] => 75
[science] => 82
)
[3] => Array
(
[name] => Sakura
[mathematics] => 75
[science] => 76
)
[4] => Array
(
[name] => Tarou
[mathematics] => 63
[science] => 82
)
)
大文字・小文字を区別しないで、配列を並べ替え
<pre>
<?php
$varArray = array( "White", "Black", "red", "green", "blue" );
$varArray_lowercase = array_map( "strtolower", $varArray );
array_multisort( $varArray_lowercase, SORT_ASC, SORT_STRING, $varArray );
print_r( $varArray );
?>
</pre>
<?php
$varArray = array( "White", "Black", "red", "green", "blue" );
$varArray_lowercase = array_map( "strtolower", $varArray );
array_multisort( $varArray_lowercase, SORT_ASC, SORT_STRING, $varArray );
print_r( $varArray );
?>
</pre>
↓↓↓出力結果↓↓↓
Array
(
[0] => Black
[1] => blue
[2] => green
[3] => red
[4] => White
)
(
[0] => Black
[1] => blue
[2] => green
[3] => red
[4] => White
)