Martha's CIS 52 Lab #2 Exercise #2

Lab #2 Exercise #2.  Create a multidimensional array of movies organized by genre.  This should take the form of an associative array with genres as keys, such as Musical, Action, Adventure, and so forth.  Each of the array’s elements should be an array containing movie names, such as Passion of Christ, Star Wars, Chariots of Fire, and so on.  When your arrays are created, loop through them, printing the name of each genre and its associated movie(s).  Sort your array.

**************************************************************

CODES:

<?php
echo "Creating the multidimensional associative array $movies using the <b>array() function</b>: <br />";
$genres = array( "musicals" => array("Oklahoma", "The Music Man", "South Pacific"),
"dramas" => array("Lawrence of Arabia", "To Kill a Mockingbird", "Casablanca"),
"mysteries" => array("The Maltese Falcon", "Rear Window", "North by Northwest"));
// print_r($genres);
echo "Printing the multidimensional associative array $movies created by the <b>array() function</b>, using <b>print_r()</b>:<br />"; print_r($genres);
echo "<br /><br />Printing the multidimensional associative array $movies created by the <b>array() function</b>, using a <b>foreach()</b> loop:<br />";
foreach ($genres as $key=>$value) {
// SOURCE: http://www.phpf1.com/tutorial/php-multidimensional-array.html?page=3
echo (strtoupper($key))."<br />";
foreach ($value as $iKey => $iValue) {
echo " ----> $iKey = $iValue<br />";
}
}
echo "Sorting the multidimensional associative array by the genres: <br />";
function compare($x, $y)
{ // SOURCE: http://www.informit.com/articles/printerfriendly.aspx?p=31840
if ( $x[1] == $y[1] )
return 0;
else if ( $x[1] > $y[1] )
return -1;
else
return 1;
}
function compare2($x2, $y2) {
if ($x[2] == $y[2])
return 0;
elseif ($x[2] > $y[2])
return -1;
else
return 1;
}
usort($genres, 'compare');
usort($genres, 'compare2');
echo "Printing the multidimensional associative array after being sorted: <br />";
foreach ($genres as $key => $value) {
echo "Movie Genre Values for Key #".$key.":<br />";
foreach ($value as $iKey => $iValue) {
echo " ----> $iKey = $iValue<br />";
}
}
// OOPS, I still don't have the movie titles sorted...
?>

**************************************************************

ANSWER:

Creating the multidimensional associative array using the array() function:
Printing the multidimensional associative array created by the array() function, using print_r():
Array ( [musicals] => Array ( [0] => Oklahoma [1] => The Music Man [2] => South Pacific ) [dramas] => Array ( [0] => Lawrence of Arabia [1] => To Kill a Mockingbird [2] => Casablanca ) [mysteries] => Array ( [0] => The Maltese Falcon [1] => Rear Window [2] => North by Northwest ) )

Printing the multidimensional associative array created by the array() function, using a foreach() loop:
MUSICALS
----> 0 = Oklahoma
----> 1 = The Music Man
----> 2 = South Pacific
DRAMAS
----> 0 = Lawrence of Arabia
----> 1 = To Kill a Mockingbird
----> 2 = Casablanca
MYSTERIES
----> 0 = The Maltese Falcon
----> 1 = Rear Window
----> 2 = North by Northwest
Sorting the multidimensional associative array by the genres:
Printing the multidimensional associative array after being sorted:
Movie Genre Values for Key #0:
----> 0 = Lawrence of Arabia
----> 1 = To Kill a Mockingbird
----> 2 = Casablanca
Movie Genre Values for Key #1:
----> 0 = Oklahoma
----> 1 = The Music Man
----> 2 = South Pacific
Movie Genre Values for Key #2:
----> 0 = The Maltese Falcon
----> 1 = Rear Window
----> 2 = North by Northwest

**************************************************************

RESOURCES:

Here are some references on multidimensional arrays and associative arrays:

http://www.informit.com/articles/article.aspx?p=31840&seqNum=3

http://www.tizag.com/phpT/arrays.php

http://www.w3schools.com/php/php_arrays.asp

http://www.webcheatsheet.com/PHP/multidimensional_arrays.php

http://onlamp.com/pub/a/php/2001/06/21/php_foundations.html

http://www.informit.com/articles/article.aspx?p=31840&seqNum=4

http://www.php-learn-it.com/php_arrays.html

http://www.php.net/manual/en/language.types.array.php

There are many more sites on the web that you can google regarding multidimensional arrays and associative arrays.