array_multisort

(PHP 4, PHP 5)

array_multisort -- 複数の多次元の配列をソートする

説明

bool array_multisort ( array ar1 [, mixed arg [, mixed ... [, array ...]]] )

array_multisort()は、複数の配列を一度に、また は、多次元の配列をその次元の一つでソートする際に使用可能です。 この関数は、ソートの際にキーの相関を維持します。

連想配列のキー(string)は不変ですが、数値添字は再度振り 直されます。

入力の配列は、あるテーブルのカラムで行をソートする処理が行われま す。これは、SQL ORDER BY構文と似ています。最初の配列はソートされ る最初の配列です。その配列の行が同じだった場合は、次の入力配列で ソートされるといったようになります。

この関数の引数の構造は、やや一般的ではありませんが、柔軟なもので す。先頭の引数は、配列である必要があります。その後の各引数には、 配列または次のリストにあるソート用フラグのどちらかを指定すること が可能です。

ソート方法指定フラグ:

ソート型のフラグ:

各配列に同じ型のソート用フラグを二つ指定することは出来ません。 ある引数配列に適用を指定されたソート用フラグが適用されるのは、そ の配列のみです。新しい配列引数を処理する前にデフォルトの SORT_ASC およびSORT_REGULARにリセットします。

例 1. 複数の配列をソートする

<?php
$ar1
= array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

この例では、ソートの後で、最初の配列は、10,"a", 100, 100となりま す。2番目の配列は、1, 1, "2", 3を有します。最初の配列(100および 100)の同じエントリに対応している2番目の配列のエントリは同時にソー トされます。

array(4) {
  [0]=> string(2) "10"
  [1]=> string(1) "a"
  [2]=> int(100)
  [3]=> int(100)
}
array(4) {
  [0]=> int(1)
  [1]=> int(1)
  [2]=> string(1) "2"
  [3]=> int(3)
}

例 2. 多次元の配列をソートする

<?php
$ar
= array (array ("10", 100, 100, "a"), array (1, 3, "2", 1));
array_multisort ($ar[0], SORT_ASC, SORT_STRING,
                 
$ar[1], SORT_NUMERIC, SORT_DESC);
?>

この例では、ソートされた後、最初の配列は10,100, 100, "a" (文字列 として昇順でソートされています)を有しており、2番目の配列は、 1, 3, "2", 1 (数値として降順にソートされています)となっています。

array(2) {
  [0]=> array(4) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> string(1) "a"
  }
  [1]=> array(4) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(1)
  }
}

例 3. Sorting database results

For this example, each element in the data array represents one row in a table. This type of dataset is typical of database records.

Example data:

volume | edition
-------+--------
    67 |       2
    86 |       1
    85 |       6
    98 |       2
    86 |       6
    67 |       7

The data as an array, called data. This would usually, for example, be obtained by looping with mysql_fetch_assoc().

<?php
$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);
?>

In this example, we will order by volume descending, edition ascending.

We have an array of rows, but array_multisort() requires an array of columns, so we use the the below code to obtain the columns, then perform the sorting.

<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
    
$volume[$key]  = $row['volume'];
    
$edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
?>

The dataset is now sorted, and will look like this:

volume | edition
-------+--------
    98 |       2
    86 |       1
    86 |       6
    85 |       6
    67 |       2
    67 |       7

例 4. Case insensitive sorting

Both SORT_STRING and SORT_REGULAR are case sensitive, strings starting with a capital letter will come before strings starting with a lowercase letter.

To perform a case insensitve search, force the sorting order to be determined by a lowercase copy of the original array.

$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);

array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);

print_r($array);

上の例の出力は以下となります:

Array
(
    [0] => Alpha
    [1] => atomic
    [2] => bank
    [3] => Beta
)