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:**************************************************************
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.