Arrays Revisited (array_multisort)

Populate three arrays with book data

Show the original state of the arrays ======================

Title data

Array
(
    [0] => The Grapes of Wrath
    [1] => The Trial
    [2] => The Hobbit
    [3] => Tale of Two Cities
)

Author data

Array
(
    [0] => John Steinbeck
    [1] => Frances Kafka
    [2] => J. R. Tolkien
    [3] => Charles Dickens
)

Published Date data

Array
(
    [0] => 1939
    [1] => 1925
    [2] => 1937
    [3] => 1859
)

Sort arrays by Title ======================

Title data

Array
(
    [0] => Tale of Two Cities
    [1] => The Grapes of Wrath
    [2] => The Hobbit
    [3] => The Trial
)

Author data

Array
(
    [0] => Charles Dickens
    [1] => John Steinbeck
    [2] => J. R. Tolkien
    [3] => Frances Kafka
)

Published Date data

Array
(
    [0] => 1859
    [1] => 1939
    [2] => 1937
    [3] => 1925
)

Sort arrays by Author ======================

Title data

Array
(
    [0] => Tale of Two Cities
    [1] => The Trial
    [2] => The Hobbit
    [3] => The Grapes of Wrath
)

Author data

Array
(
    [0] => Charles Dickens
    [1] => Frances Kafka
    [2] => J. R. Tolkien
    [3] => John Steinbeck
)

Published Date data

Array
(
    [0] => 1859
    [1] => 1925
    [2] => 1937
    [3] => 1939
)

Sort arrays by Date Published ======================

Title data

Array
(
    [0] => Tale of Two Cities
    [1] => The Trial
    [2] => The Hobbit
    [3] => The Grapes of Wrath
)

Author data

Array
(
    [0] => Charles Dickens
    [1] => Frances Kafka
    [2] => J. R. Tolkien
    [3] => John Steinbeck
)

Published Date data

Array
(
    [0] => 1859
    [1] => 1925
    [2] => 1937
    [3] => 1939
)

 

CODE FOLLOWS

<?php
include('includes/header.php');

// code
error_reporting(E_ALL);    // set error reporting to all

echo('<h3>Arrays Revisited (array_multisort)</h3>');

echo(
'<p>Populate three arrays with book data</p>');

// populate arrays
$titles = array('The Grapes of Wrath','The Trial','The Hobbit','Tale of Two Cities');
$authors = array('John Steinbeck','Frances Kafka''J. R. Tolkien','Charles Dickens');
$published = array('1939','1925','1937','1859');


echo(
'<p>Show the original state of the arrays ======================</p>');

echo(
'<p>Title data</p>');

echo(
'<pre>');
print_r($titles);
echo(
'</pre>');

echo(
'<p>Author data</p>');

echo(
'<pre>');
print_r($authors);
echo(
'</pre>');

echo(
'<p>Published Date data</p>');

echo(
'<pre>');
print_r($published);
echo(
'</pre>');
echo(
'<br>');

echo(
'<p>Sort arrays by Title ======================</p>');
array_multisort($titles$authors$published);

echo(
'<p>Title data</p>');

echo(
'<pre>');
print_r($titles);
echo(
'</pre>');

echo(
'<p>Author data</p>');

echo(
'<pre>');
print_r($authors);
echo(
'</pre>');

echo(
'<p>Published Date data</p>');

echo(
'<pre>');
print_r($published);
echo(
'</pre>');
echo(
'<br>');

echo(
'<p>Sort arrays by Author ======================</p>');
array_multisort($authors$titles$published);

echo(
'<p>Title data</p>');

echo(
'<pre>');
print_r($titles);
echo(
'</pre>');

echo(
'<p>Author data</p>');

echo(
'<pre>');
print_r($authors);
echo(
'</pre>');

echo(
'<p>Published Date data</p>');

echo(
'<pre>');
print_r($published);
echo(
'</pre>');
echo(
'<br>');

echo(
'<p>Sort arrays by Date Published ======================</p>');
array_multisort($published$titles$authors);

echo(
'<p>Title data</p>');

echo(
'<pre>');
print_r($titles);
echo(
'</pre>');

echo(
'<p>Author data</p>');

echo(
'<pre>');
print_r($authors);
echo(
'</pre>');

echo(
'<p>Published Date data</p>');

echo(
'<pre>');
print_r($published);
echo(
'</pre>');
echo(
'<br>');

include(
'includes/footer.php');
?>