1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-25 23:52:16 +01:00
Files
archived-doc-ja/reference/array/functions/array-multisort.xml
Rui Hirokawa b76980f132 sync with en.
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@174810 c90b9560-bf6c-de11-be94-00142212c4b1
2004-12-15 16:00:27 +00:00

291 lines
8.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 1.13 Maintainer: hirokawa Status: working -->
<refentry id="function.array-multisort">
<refnamediv>
<refname>array_multisort</refname>
<refpurpose>複数の多次元の配列をソートする</refpurpose>
</refnamediv>
<refsect1>
<title>説明</title>
<methodsynopsis>
<type>bool</type><methodname>array_multisort</methodname>
<methodparam><type>array</type><parameter>ar1</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>arg</parameter></methodparam>
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>...</parameter></methodparam>
<!-- Parameters don't need to be passed by reference -->
</methodsynopsis>
<para>
<function>array_multisort</function>は、複数の配列を一度に、また
は、多次元の配列をその次元の一つでソートする際に使用可能です。
この関数は、ソートの際にキーの相関を維持します。
</para>
<para>
連想配列のキー(<type>string</type>)は不変ですが、数値添字は再度振り
直されます。
</para>
<para>
入力の配列は、あるテーブルのカラムで行をソートする処理が行われま
す。これは、SQL ORDER BY構文と似ています。最初の配列はソートされ
る最初の配列です。その配列の行が同じだった場合は、次の入力配列で
ソートされるといったようになります。
</para>
<para>
この関数の引数の構造は、やや一般的ではありませんが、柔軟なもので
す。先頭の引数は、配列である必要があります。その後の各引数には、
配列または次のリストにあるソート用フラグのどちらかを指定すること
が可能です。
</para>
<para>
ソート方法指定フラグ:
<itemizedlist>
<listitem>
<simpara><constant>SORT_ASC</constant> - 昇順にソート</simpara>
</listitem>
<listitem>
<simpara><constant>SORT_DESC</constant> - 降順にソート</simpara>
</listitem>
</itemizedlist>
</para>
<para>
ソート型のフラグ:
<itemizedlist>
<listitem>
<simpara><constant>SORT_REGULAR</constant> - 普通に比較</simpara>
</listitem>
<listitem>
<simpara><constant>SORT_NUMERIC</constant> - 数値的に比較</simpara>
</listitem>
<listitem>
<simpara><constant>SORT_STRING</constant> - 文字列として比較</simpara>
</listitem>
</itemizedlist>
</para>
<para>
各配列に同じ型のソート用フラグを二つ指定することは出来ません。
ある引数配列に適用を指定されたソート用フラグが適用されるのは、そ
の配列のみです。新しい配列引数を処理する前にデフォルトの
<constant>SORT_ASC</constant>
および<constant>SORT_REGULAR</constant>にリセットします。
</para>
<para>
<example>
<title>複数の配列をソートする</title>
<programlisting role="php">
<![CDATA[
<?php
$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
]]>
</programlisting>
<para>
この例では、ソートの後で、最初の配列は、10,"a", 100, 100となりま
す。2番目の配列は、1, 1, "2", 3を有します。最初の配列(100および
100)の同じエントリに対応している2番目の配列のエントリは同時にソー
トされます。
</para>
<screen>
<![CDATA[
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)
}
]]>
</screen>
</example>
</para>
<para>
<example>
<title>多次元の配列をソートする</title>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
<para>
この例では、ソートされた後、最初の配列は10,100, 100, "a" (文字列
として昇順でソートされています)を有しており、2番目の配列は、
1, 3, "2", 1 (数値として降順にソートされています)となっています。
</para>
<screen>
<![CDATA[
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)
}
}
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Sorting database results</title>
<para>
For this example, each element in the <varname>data</varname>
array represents one row in a table. This type of dataset is typical
of database records.
</para>
<para>
Example data:
</para>
<screen>
<![CDATA[
volume | edition
-------+--------
67 | 2
86 | 1
85 | 6
98 | 2
86 | 6
67 | 7
]]>
</screen>
<para>
The data as an array, called <varname>data</varname>. This would usually,
for example, be obtained by looping with <function>mysql_fetch_assoc</function>.
</para>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
<para>
In this example, we will order by <varname>volume</varname> descending,
<varname>edition</varname> ascending.
</para>
<para>
We have an array of rows, but <function>array_multisort</function>
requires an array of columns, so we use the the below code to obtain the
columns, then perform the sorting.
</para>
<programlisting role="php">
<![CDATA[
<?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);
?>
]]>
</programlisting>
<para>
The dataset is now sorted, and will look like this:
</para>
<screen>
<![CDATA[
volume | edition
-------+--------
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7
]]>
</screen>
</example>
</para>
<para>
<example>
<title>Case insensitive sorting</title>
<para>
Both <constant>SORT_STRING</constant> and
<constant>SORT_REGULAR</constant> are case sensitive, strings
starting with a capital letter will come before strings starting
with a lowercase letter.
</para>
<para>
To perform a case insensitve search, force the sorting order to be
determined by a lowercase copy of the original array.
</para>
<programlisting role="php">
<![CDATA[
$array = array('Alpha', 'atomic', 'Beta', 'bank');
$array_lowercase = array_map('strtolower', $array);
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
print_r($array);
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Array
(
[0] => Alpha
[1] => atomic
[2] => bank
[3] => Beta
)
]]>
</screen>
</example>
</para>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->