2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:30:45 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.array-multisort" xmlns="http://docbook.org/ns/docbook">
|
2006-10-31 11:24:02 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>array_multisort</refname>
|
|
|
|
<refpurpose>Sort multiple or multi-dimensional arrays</refpurpose>
|
|
|
|
</refnamediv>
|
2007-12-30 23:05:06 +00:00
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2006-10-31 11:24:02 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>bool</type><methodname>array_multisort</methodname>
|
2013-09-24 13:06:51 +00:00
|
|
|
<methodparam><type>array</type><parameter role="reference">array1</parameter></methodparam>
|
|
|
|
<methodparam choice="opt"><type>mixed</type><parameter>array1_sort_order</parameter><initializer>SORT_ASC</initializer></methodparam>
|
|
|
|
<methodparam choice="opt"><type>mixed</type><parameter>array1_sort_flags</parameter><initializer>SORT_REGULAR</initializer></methodparam>
|
2020-11-02 11:17:26 +00:00
|
|
|
<methodparam rep="repeat"><type>mixed</type><parameter>rest</parameter></methodparam>
|
2006-10-31 11:24:02 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
<function>array_multisort</function> can be used to sort several
|
|
|
|
arrays at once, or a multi-dimensional array by one or more
|
|
|
|
dimensions.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Associative (<type>string</type>) keys will be maintained, but numeric
|
|
|
|
keys will be re-indexed.
|
|
|
|
</para>
|
2016-10-08 09:12:17 +00:00
|
|
|
¬e.sort-unstable;
|
2007-12-30 23:05:06 +00:00
|
|
|
</refsect1>
|
2008-02-21 01:36:14 +00:00
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2013-09-24 13:06:51 +00:00
|
|
|
<term><parameter>array1</parameter></term>
|
2008-02-21 01:36:14 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
An <type>array</type> being sorted.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2013-09-24 13:06:51 +00:00
|
|
|
<term><parameter>array1_sort_order</parameter></term>
|
2008-02-21 01:36:14 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2013-09-24 13:06:51 +00:00
|
|
|
The order used to sort the previous <type>array</type> argument. Either
|
|
|
|
<constant>SORT_ASC</constant> to sort ascendingly or <constant>SORT_DESC</constant>
|
|
|
|
to sort descendingly.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This argument can be swapped with <parameter>array1_sort_flags</parameter>
|
|
|
|
or omitted entirely, in which case <constant>SORT_ASC</constant> is assumed.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>array1_sort_flags</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Sort options for the previous <type>array</type> argument:
|
2013-01-17 14:12:24 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Sorting type flags:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<simpara><constant>SORT_REGULAR</constant> - compare items normally
|
|
|
|
(don't change types)</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara><constant>SORT_NUMERIC</constant> - compare items numerically</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara><constant>SORT_STRING</constant> - compare items as strings</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<constant>SORT_LOCALE_STRING</constant> - compare items as
|
|
|
|
strings, based on the current locale. It uses the locale,
|
|
|
|
which can be changed using <function>setlocale</function>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<constant>SORT_NATURAL</constant> - compare items as strings
|
|
|
|
using "natural ordering" like <function>natsort</function>
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<constant>SORT_FLAG_CASE</constant> - can be combined
|
|
|
|
(bitwise OR) with
|
|
|
|
<constant>SORT_STRING</constant> or
|
|
|
|
<constant>SORT_NATURAL</constant> to sort strings case-insensitively
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
2008-02-21 01:36:14 +00:00
|
|
|
</para>
|
2013-09-24 13:06:51 +00:00
|
|
|
<para>
|
|
|
|
This argument can be swapped with <parameter>array1_sort_order</parameter>
|
|
|
|
or omitted entirely, in which case <constant>SORT_REGULAR</constant> is assumed.
|
|
|
|
</para>
|
2008-02-21 01:36:14 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2020-11-02 11:17:26 +00:00
|
|
|
<term><parameter>rest</parameter></term>
|
2008-02-21 01:36:14 +00:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2015-05-08 22:03:39 +00:00
|
|
|
More arrays, optionally followed by sort order and flags. Only elements
|
|
|
|
corresponding to equivalent elements in previous arrays are compared.
|
|
|
|
In other words, the sort is lexicographical.
|
2008-02-21 01:36:14 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2007-12-30 23:05:06 +00:00
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
2006-10-31 11:24:02 +00:00
|
|
|
<para>
|
2007-12-30 23:05:06 +00:00
|
|
|
&return.success;
|
2006-10-31 11:24:02 +00:00
|
|
|
</para>
|
2007-12-30 23:05:06 +00:00
|
|
|
</refsect1>
|
2015-05-08 22:03:39 +00:00
|
|
|
|
2007-12-30 23:05:06 +00:00
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2006-10-31 11:24:02 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Sorting multiple arrays</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2007-11-21 15:01:30 +00:00
|
|
|
$ar1 = array(10, 100, 100, 0);
|
|
|
|
$ar2 = array(1, 3, 2, 4);
|
2003-08-17 12:21:03 +00:00
|
|
|
array_multisort($ar1, $ar2);
|
2004-10-03 11:06:39 +00:00
|
|
|
|
|
|
|
var_dump($ar1);
|
|
|
|
var_dump($ar2);
|
2003-05-30 18:12:53 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
2007-11-21 15:01:30 +00:00
|
|
|
In this example, after sorting, the first array will contain 0,
|
|
|
|
10, 100, 100. The second array will contain 4, 1, 2, 3. The
|
2006-10-31 11:24:02 +00:00
|
|
|
entries in the second array corresponding to the identical
|
|
|
|
entries in the first array (100 and 100) were sorted as well.
|
|
|
|
</para>
|
|
|
|
<screen>
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
array(4) {
|
2007-11-21 15:01:30 +00:00
|
|
|
[0]=> int(0)
|
|
|
|
[1]=> int(10)
|
2004-10-03 11:06:39 +00:00
|
|
|
[2]=> int(100)
|
|
|
|
[3]=> int(100)
|
|
|
|
}
|
|
|
|
array(4) {
|
2007-11-21 15:01:30 +00:00
|
|
|
[0]=> int(4)
|
2004-10-03 11:06:39 +00:00
|
|
|
[1]=> int(1)
|
2007-11-21 15:01:30 +00:00
|
|
|
[2]=> int(2)
|
2004-10-03 11:06:39 +00:00
|
|
|
[3]=> int(3)
|
|
|
|
}
|
2006-10-31 11:24:02 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Sorting multi-dimensional array</title>
|
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
2003-05-30 18:12:53 +00:00
|
|
|
<?php
|
2005-04-05 18:27:08 +00:00
|
|
|
$ar = array(
|
2006-10-31 11:24:02 +00:00
|
|
|
array("10", 11, 100, 100, "a"),
|
2005-04-05 18:27:08 +00:00
|
|
|
array( 1, 2, "2", 3, 1)
|
|
|
|
);
|
2003-08-17 12:21:03 +00:00
|
|
|
array_multisort($ar[0], SORT_ASC, SORT_STRING,
|
|
|
|
$ar[1], SORT_NUMERIC, SORT_DESC);
|
2005-04-05 18:27:08 +00:00
|
|
|
var_dump($ar);
|
2003-05-30 18:12:53 +00:00
|
|
|
?>
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
In this example, after sorting, the first array will transform to
|
|
|
|
"10", 100, 100, 11, "a" (it was sorted as strings in ascending
|
|
|
|
order). The second will contain 1, 3, "2", 2, 1 (sorted as numbers,
|
|
|
|
in descending order).
|
|
|
|
</para>
|
|
|
|
<screen>
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
array(2) {
|
2005-04-05 18:27:08 +00:00
|
|
|
[0]=> array(5) {
|
2004-10-03 11:06:39 +00:00
|
|
|
[0]=> string(2) "10"
|
|
|
|
[1]=> int(100)
|
|
|
|
[2]=> int(100)
|
2005-04-05 18:27:08 +00:00
|
|
|
[3]=> int(11)
|
|
|
|
[4]=> string(1) "a"
|
2004-10-03 11:06:39 +00:00
|
|
|
}
|
2005-04-05 18:27:08 +00:00
|
|
|
[1]=> array(5) {
|
2004-10-03 11:06:39 +00:00
|
|
|
[0]=> int(1)
|
|
|
|
[1]=> int(3)
|
|
|
|
[2]=> string(1) "2"
|
2005-04-05 18:27:08 +00:00
|
|
|
[3]=> int(2)
|
|
|
|
[4]=> int(1)
|
2004-10-03 11:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</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>
|
2004-10-03 11:06:39 +00:00
|
|
|
<para>
|
2006-10-31 11:24:02 +00:00
|
|
|
Example data:
|
|
|
|
</para>
|
|
|
|
<screen>
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
volume | edition
|
|
|
|
-------+--------
|
|
|
|
67 | 2
|
|
|
|
86 | 1
|
|
|
|
85 | 6
|
|
|
|
98 | 2
|
|
|
|
86 | 6
|
|
|
|
67 | 7
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
<para>
|
|
|
|
The data as an array, called <varname>data</varname>. This would usually,
|
2016-07-10 12:39:45 +00:00
|
|
|
for example, be obtained by looping with <function>mysqli_fetch_assoc</function>.
|
2006-10-31 11:24:02 +00:00
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2004-10-03 11:06:39 +00:00
|
|
|
<![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);
|
|
|
|
?>
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</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 below code to obtain the
|
|
|
|
columns, then perform the sorting.
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
// Obtain a list of columns
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
$volume[$key] = $row['volume'];
|
|
|
|
$edition[$key] = $row['edition'];
|
|
|
|
}
|
|
|
|
|
2018-07-14 12:22:09 +00:00
|
|
|
// as of PHP 5.5.0 you can use array_column() instead of the above code
|
|
|
|
$volume = array_column($data, 'volume');
|
|
|
|
$edition = array_column($data, 'edition');
|
|
|
|
|
2004-10-03 11:06:39 +00:00
|
|
|
// 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);
|
|
|
|
?>
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
<para>
|
|
|
|
The dataset is now sorted, and will look like this:
|
|
|
|
</para>
|
|
|
|
<screen>
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
volume | edition
|
|
|
|
-------+--------
|
|
|
|
98 | 2
|
|
|
|
86 | 1
|
|
|
|
86 | 6
|
|
|
|
85 | 6
|
|
|
|
67 | 2
|
|
|
|
67 | 7
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</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>
|
2002-04-15 00:12:54 +00:00
|
|
|
<para>
|
2016-06-29 16:45:26 +00:00
|
|
|
To perform a case insensitive sort, force the sorting order to be
|
2006-10-31 11:24:02 +00:00
|
|
|
determined by a lowercase copy of the original array.
|
|
|
|
</para>
|
|
|
|
<programlisting role="php">
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
2004-12-20 02:15:13 +00:00
|
|
|
<?php
|
2004-10-03 11:06:39 +00:00
|
|
|
$array = array('Alpha', 'atomic', 'Beta', 'bank');
|
|
|
|
$array_lowercase = array_map('strtolower', $array);
|
|
|
|
|
2004-10-08 08:43:04 +00:00
|
|
|
array_multisort($array_lowercase, SORT_ASC, SORT_STRING, $array);
|
2004-10-03 11:06:39 +00:00
|
|
|
|
|
|
|
print_r($array);
|
2004-12-20 02:15:13 +00:00
|
|
|
?>
|
2004-10-03 11:06:39 +00:00
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</programlisting>
|
|
|
|
&example.outputs;
|
|
|
|
<screen>
|
2004-10-03 11:06:39 +00:00
|
|
|
<![CDATA[
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => Alpha
|
|
|
|
[1] => atomic
|
|
|
|
[2] => bank
|
|
|
|
[3] => Beta
|
|
|
|
)
|
|
|
|
]]>
|
2006-10-31 11:24:02 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
2009-03-20 20:40:34 +00:00
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
|
|
|
<member><function>usort</function></member>
|
|
|
|
<member>&seealso.array.sorting;</member>
|
|
|
|
</simplelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2006-10-31 11:24:02 +00:00
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
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
|
|
|
|
-->
|