magicadey Posted March 31, 2008 Share Posted March 31, 2008 I'm a bit stuck on this question 1, Create a multidimensional array of movies organized by genre. This should take the form of an associative array with genres as keys ("SF", "Action", "Romance", and so on). Each of this associative array's elements should be an array containing movie names ("2001", "Alien", "Terminator", and so on). 2, Loop through the array you created in exercise 1, outputting each genre and its associated movies to the browser. I dunno how to get it so it displays something like: Horror - list of horror films War -List of war films my array looks like this. Probably wrong to do what the question says $films = array( array( "sf"=>"Alien", "horror"=>"Hellraiser", "war"=>"platoon", ), array( "sf"=>"star wars", "horror"=>"dawn of the dead", "war"=>"1942", ), ); Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/ Share on other sites More sharing options...
Psycho Posted March 31, 2008 Share Posted March 31, 2008 Well, this is obviously an assignment, so I will not give you the solution. But, your array is incorrect to begin with. It states that "This should take the form of an associative array with genres as keys " Your example above has no defined keys for the topmost array elements - you simply created sub-arrays which will have the indexes 0, 1, 2, etc. The array should look more like this: $films = array ( 'Horror' => array ('film1', 'film2', 'film3'), 'Drama' => array ('film1', 'film2', 'film3'), 'Action' => array ('film1', 'film2', 'film3') ); Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505792 Share on other sites More sharing options...
rhodesa Posted March 31, 2008 Share Posted March 31, 2008 Here is some info on how to do the loop: http://us2.php.net/foreach Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505797 Share on other sites More sharing options...
magicadey Posted March 31, 2008 Author Share Posted March 31, 2008 not really assignment. learning using the book teach yourself php!! ok sorted it out : managed to sort the topics but i was wondering if it were possible to do another sort for the film names within each topic <?php $films = array( "Horror" => array( "Hellraiser", "Dawn Of The Dead", "The Evil Dead", ), "War"=>array( "Saving Private Ryan", "Platoon", "1942", ), "Action"=>array( "James Bond", "Die Hard", "Terminator", "The Matrix", ), ); ksort ($films); foreach ($films as $title => $items){ print "<p>"; print "<u>$title</u><br/>"; foreach ($items as $key=> $name ) { print "$name<br/>"; } print "</p>"; } ?> </div> </body> </html> output: Action James Bond Die Hard Terminator The Matrix Horror Hellraiser Dawn Of The Dead The Evil Dead War Saving Private Ryan Platoon 1942 Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505886 Share on other sites More sharing options...
BlueSkyIS Posted March 31, 2008 Share Posted March 31, 2008 asort($array); Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505890 Share on other sites More sharing options...
rhodesa Posted March 31, 2008 Share Posted March 31, 2008 Just before you loop over $items, sort it using sort() Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505902 Share on other sites More sharing options...
magicadey Posted March 31, 2008 Author Share Posted March 31, 2008 ahh cheers.. Quote Link to comment https://forums.phpfreaks.com/topic/98845-php-in-24-hours-exercise-help/#findComment-505907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.