@@ -234,18 +234,6 @@ volume | edition
234234 The data as an array, called <varname >data</varname >. This would usually,
235235 for example, be obtained by looping with <function >mysqli_fetch_assoc</function >.
236236 </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 >
249237 <para >
250238 In this example, we will order by <varname >volume</varname > descending,
251239 <varname >edition</varname > ascending.
@@ -258,19 +246,34 @@ $data[] = array('volume' => 67, 'edition' => 7);
258246 <programlisting role =" php" >
259247<![CDATA[
260248<?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+
261257// Obtain a list of columns
262258foreach ($data as $key => $row) {
263259 $volume[$key] = $row['volume'];
264260 $edition[$key] = $row['edition'];
265261}
266262
267- // you can use array_column() instead of the above code
263+ // You can use array_column() instead of the above code
268264$volume = array_column($data, 'volume');
269265$edition = array_column($data, 'edition');
270266
271267// Sort the data with volume descending, edition ascending
272268// Add $data as the last parameter, to sort by the common key
273269array_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+ }
274277?>
275278]]>
276279 </programlisting >
0 commit comments