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 Hobit
    [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 Hobit
    [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 Hobit
    [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 Hobit
    [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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Arrays Revisited</title>
    <link rel="stylesheet" href="../css/styles.css">
</head>
<body>

<div class="page">

    <p class="nav"><a href="../../index.php">Home</a></p>
    <?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 Hobit','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>');
    
?>

    <hr>

    <h2>Code follows</h2>

    <div class="code"><?php highlight_file(__FILE__); ?></div>

    <p class="nav nav-foot"><a href="../../index.php">Home</a></p>

</div>

</body>
</html>