File: array-multisort.xml

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (304 lines) | stat: -rw-r--r-- 8,284 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.17 $ -->
<!-- splitted from ./en/functions/array.xml, last change in rev 1.11 -->
  <refentry id="function.array-multisort">
   <refnamediv>
    <refname>array_multisort</refname>
    <refpurpose>Sort multiple or multi-dimensional arrays</refpurpose>
   </refnamediv>
   <refsect1>
    <title>Description</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>
     &return.success;
    </para>
    <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>
    <para>
     The input arrays are treated as columns of a table to be sorted
     by rows - this resembles the functionality of SQL ORDER BY
     clause. The first array is the primary one to sort by. The rows
     (values) in that array that compare the same are sorted by the
     next input array, and so on.
    </para>
    <para>
     The argument structure of this function is a bit unusual, but
     flexible. The first argument has to be an array. Subsequently,
     each argument can be either an array or a sorting flag from the
     following lists.
    </para>
    <para>
     Sorting order flags:
     <itemizedlist>
      <listitem>
       <simpara><constant>SORT_ASC</constant> - Sort in ascending order</simpara>
      </listitem>
      <listitem>
       <simpara><constant>SORT_DESC</constant> - Sort in descending order</simpara>
      </listitem>
     </itemizedlist>
    </para>
    <para>
     Sorting type flags:
     <itemizedlist>
      <listitem>
       <simpara><constant>SORT_REGULAR</constant> - Compare items normally</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>
     </itemizedlist>
    </para>
    <para>
     No two sorting flags of the same type can be specified after each
     array. The sorting flags specified after an array argument apply
     only to that array - they are reset to default <constant>SORT_ASC</constant> and
     <constant>SORT_REGULAR</constant> before each new array argument.
    </para>

    <para>
     <example>
      <title>Sorting multiple arrays</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>
       In this example, after sorting, the first array will contain "10",
       "a", 100, 100. The second array will contain 1, 1, "2", 3. The
       entries in the second array corresponding to the identical
       entries in the first array (100 and 100) were sorted as well.
      </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>Sorting multi-dimensional array</title>
      <programlisting role="php">
<![CDATA[
<?php
$ar = array(
       array("10", 11, 100, 100, "a"), 
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
]]>
      </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>
<![CDATA[
array(2) {
  [0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> 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 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 insensitive search, force the sorting order to be
       determined by a lowercase copy of the original array.
      </para>
      <programlisting role="php">
<![CDATA[
<?php
$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
-->