@@ -234,18 +234,6 @@ volume | edition
234
234
The data as an array, called <varname >data</varname >. This would usually,
235
235
for example, be obtained by looping with <function >mysqli_fetch_assoc</function >.
236
236
</para >
237
- <programlisting role =" php" >
238
- <![CDATA[
239
- <?php
240
- $data[] = array('volume' => 67, 'edition' => 2);
241
- $data[] = array('volume' => 86, 'edition' => 1);
242
- $data[] = array('volume' => 85, 'edition' => 6);
243
- $data[] = array('volume' => 98, 'edition' => 2);
244
- $data[] = array('volume' => 86, 'edition' => 6);
245
- $data[] = array('volume' => 67, 'edition' => 7);
246
- ?>
247
- ]]>
248
- </programlisting >
249
237
<para >
250
238
In this example, we will order by <varname >volume</varname > descending,
251
239
<varname >edition</varname > ascending.
@@ -258,19 +246,34 @@ $data[] = array('volume' => 67, 'edition' => 7);
258
246
<programlisting role =" php" >
259
247
<![CDATA[
260
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
+
261
257
// Obtain a list of columns
262
258
foreach ($data as $key => $row) {
263
259
$volume[$key] = $row['volume'];
264
260
$edition[$key] = $row['edition'];
265
261
}
266
262
267
- // you can use array_column() instead of the above code
263
+ // You can use array_column() instead of the above code
268
264
$volume = array_column($data, 'volume');
269
265
$edition = array_column($data, 'edition');
270
266
271
267
// Sort the data with volume descending, edition ascending
272
268
// Add $data as the last parameter, to sort by the common key
273
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
+ }
274
277
?>
275
278
]]>
276
279
</programlisting >
0 commit comments