reference/array/functions/array-multisort.xml
2e60c5134e7a847c99f81eb3f7ecee1f5efeeace
...
...
@@ -12,7 +12,7 @@
12
12
<methodparam><type>array</type><parameter role="reference">array1</parameter></methodparam>
13
13
<methodparam choice="opt"><type>mixed</type><parameter>array1_sort_order</parameter><initializer>SORT_ASC</initializer></methodparam>
14
14
<methodparam choice="opt"><type>mixed</type><parameter>array1_sort_flags</parameter><initializer>SORT_REGULAR</initializer></methodparam>
15
-
<methodparam choice="opt"><type>mixed</type><parameter>...</parameter></methodparam>
15
+
<methodparam rep="repeat"><type>mixed</type><parameter>rest</parameter></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
18
<function>array_multisort</function> can be used to sort several
...
...
@@ -24,6 +24,7 @@
24
24
keys will be re-indexed.
25
25
</para>
26
26
&note.sort-unstable;
27
+
&note.reset-index;
27
28
</refsect1>
28
29

29
30
<refsect1 role="parameters">
...
...
@@ -101,7 +102,7 @@
101
102
</listitem>
102
103
</varlistentry>
103
104
<varlistentry>
104
-
<term><parameter>...</parameter></term>
105
+
<term><parameter>rest</parameter></term>
105
106
<listitem>
106
107
<para>
107
108
More arrays, optionally followed by sort order and flags. Only elements
...
...
@@ -121,38 +122,6 @@
121
122
</para>
122
123
</refsect1>
123
124

124
-
<refsect1 role="changelog">
125
-
&reftitle.changelog;
126
-
<para>
127
-
<informaltable>
128
-
<tgroup cols="2">
129
-
<thead>
130
-
<row>
131
-
<entry>&Version;</entry>
132
-
<entry>&Description;</entry>
133
-
</row>
134
-
</thead>
135
-
<tbody>
136
-
<row>
137
-
<entry>5.4.0</entry>
138
-
<entry>
139
-
The <constant>SORT_NATURAL</constant> and <constant>SORT_FLAG_CASE</constant>
140
-
were added to <parameter>array1_sort_flags</parameter> as possible sort flags.
141
-
</entry>
142
-
</row>
143
-
<row>
144
-
<entry>5.3.0</entry>
145
-
<entry>
146
-
The <constant>SORT_LOCALE_STRING</constant> was added to
147
-
<parameter>array1_sort_flags</parameter> as possible sort flags.
148
-
</entry>
149
-
</row>
150
-
</tbody>
151
-
</tgroup>
152
-
</informaltable>
153
-
</para>
154
-
</refsect1>
155
-

156
125
<refsect1 role="examples">
157
126
&reftitle.examples;
158
127
<para>
...
...
@@ -265,18 +234,6 @@ volume | edition
265
234
The data as an array, called <varname>data</varname>. This would usually,
266
235
for example, be obtained by looping with <function>mysqli_fetch_assoc</function>.
267
236
</para>
268
-
<programlisting role="php">
269
-
<![CDATA[
270
-
<?php
271
-
$data[] = array('volume' => 67, 'edition' => 2);
272
-
$data[] = array('volume' => 86, 'edition' => 1);
273
-
$data[] = array('volume' => 85, 'edition' => 6);
274
-
$data[] = array('volume' => 98, 'edition' => 2);
275
-
$data[] = array('volume' => 86, 'edition' => 6);
276
-
$data[] = array('volume' => 67, 'edition' => 7);
277
-
?>
278
-
]]>
279
-
</programlisting>
280
237
<para>
281
238
In this example, we will order by <varname>volume</varname> descending,
282
239
<varname>edition</varname> ascending.
...
...
@@ -289,15 +246,34 @@ $data[] = array('volume' => 67, 'edition' => 7);
289
246
<programlisting role="php">
290
247
<![CDATA[
291
248
<?php
249
+
// The data as created by looping over mysqli_fetch_assoc:
250
+
$data[] = array('volume' => 67, 'edition' => 2);
251
+
$data[] = array('volume' => 86, 'edition' => 1);
252
+
$data[] = array('volume' => 85, 'edition' => 6);
253
+
$data[] = array('volume' => 98, 'edition' => 2);
254
+
$data[] = array('volume' => 86, 'edition' => 6);
255
+
$data[] = array('volume' => 67, 'edition' => 7);
256
+

292
257
// Obtain a list of columns
293
258
foreach ($data as $key => $row) {
294
259
$volume[$key] = $row['volume'];
295
260
$edition[$key] = $row['edition'];
296
261
}
297
262

263
+
// You can use array_column() instead of the above code
264
+
$volume = array_column($data, 'volume');
265
+
$edition = array_column($data, 'edition');
266
+

298
267
// Sort the data with volume descending, edition ascending
299
268
// Add $data as the last parameter, to sort by the common key
300
269
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
270
+

271
+
// Loop over the data and output the sorted values for each column
272
+
echo 'volume | edition', PHP_EOL;
273
+
echo '-------+--------', PHP_EOL;
274
+
for ($i = 0; $i < count($data); $i++) {
275
+
printf("%6d | %7d\n", $volume[$i], $edition[$i]);
276
+
}
301
277
?>
302
278
]]>
303
279
</programlisting>
304
280