adam1984 Posted May 4, 2009 Share Posted May 4, 2009 I'm studying PHP on my own and have came across an exercise that I'm 99% done with! After looking at the excercise in question, please help with a simple solution. All I want to do is print my genre (can't do) with its associated movies to the browser (can do, thats the easy part) EXERCISE: Create a multidimensional array of movies 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 ("Superbad", "Alien", "Terminator", and so on). <?php $genres = array ( $comedy=array("Superbad", "Yes Man", "Anchor Man"), $horror=array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"), ); foreach($genres as $section){ print "<p>"; foreach ($section as $subject=> $value){ static $subject=0; $subject++; print "$subject: $value<br />"; } print "</p>"; } ?> If you preview this on your own system, you see it's pretty simple, each element in the array is part of a numbered list. But what I WANT to do, is each time the loops goes through to print the name of the genres they are in!!!! Please Help. I thank you in advance. Your help is very much appreciated. - Adam Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/ Share on other sites More sharing options...
DarkWater Posted May 4, 2009 Share Posted May 4, 2009 You didn't set up the keys correctly in the array. <?php $genres = array ( 'Comedy' => array("Superbad", "Yes Man", "Anchor Man"), 'Horror' => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"), ); foreach($genres as $genre){ echo "<p>$genre:</p>\n<p>"; foreach ($genre as $num => $movie) { echo "$num: $movie<br />\n"; } echo "</p><br />"; } Should be good. Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826162 Share on other sites More sharing options...
Phoenix~Fire Posted May 4, 2009 Share Posted May 4, 2009 should use for... not foreach for the second one. and get rid of the $ before 'comedy' and 'horror' and echo $subject; $genres = array ( 'comedy' =>array("Superbad", "Yes Man", "Anchor Man"), 'horror' =>array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"), ); Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826166 Share on other sites More sharing options...
the182guy Posted May 4, 2009 Share Posted May 4, 2009 This works: $movies = array( 'Comedy' => array( 'Superbad', 'Yes Man', 'Anchor Man', ), 'Horror' => array( 'Texas Chainsaw', 'Saw 2' ), 'Romance' => array( 'Romance movie 1', 'Romance movie 2' ) ); foreach($movies as $genre => $movie_list) { foreach($movie_list as $movie) { echo $movie . ' ( ' . $genre . ')<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826170 Share on other sites More sharing options...
adam1984 Posted May 4, 2009 Author Share Posted May 4, 2009 Ok, I can see that some of you are trying to help me out. Can one of you give me the entire code I will need inside my php tags? To reitterate, I want the outcome to look something like this: Horror 1. Slither 2. etc. 3. etc. Comedy 1. Superbad 2. Anchorman 3. etc. Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826181 Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 <?php $genres = array ( 'Comedy' => array("Superbad", "Yes Man", "Anchor Man"), 'Horror' => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"), ); foreach($genres as $genre){ echo "<p>$genre:</p>\n<p>"; $i=1; foreach ($genre as $num => $movie) { echo "{$i}: {$movie}<br />\n"; $i++; } echo "</p><br />"; } ?> Should do it. Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826194 Share on other sites More sharing options...
adam1984 Posted May 4, 2009 Author Share Posted May 4, 2009 No Premiso, when I run your script I get: Array: 1: Superbad 2: Yes Man 3: Anchor Man Array: 1: Texas Chainsaw Massacre 2: People Under The Stairs 3: Slither -- I want it to say the name of the array (such as Comedy, and Horror above the second batch) I don't want it to just say "Array". Any more help please? Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826206 Share on other sites More sharing options...
premiso Posted May 4, 2009 Share Posted May 4, 2009 My bad. <?php $genres = array ( 'Comedy' => array("Superbad", "Yes Man", "Anchor Man"), 'Horror' => array("Texas Chainsaw Massacre", "People Under The Stairs", "Slither"), ); foreach($genres as $genre => $movies){ echo "<p>$genre:</p>\n<p>"; $i=1; foreach ($movies as $num => $movie) { echo "{$i}: {$movie}<br />\n"; $i++; } echo "</p><br />"; } ?> Should take of it. Sorry about that. Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826212 Share on other sites More sharing options...
adam1984 Posted May 4, 2009 Author Share Posted May 4, 2009 Thank you very much!!!! I really really appreciate the tips. MARK THIS BABY AS SOLVED!!!! Seriously, I just want to say I'm very thankful for the quick responses and help breaking this baby down. It really means a lot. Sincerely, Adam Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826218 Share on other sites More sharing options...
the182guy Posted May 4, 2009 Share Posted May 4, 2009 The full code was provided in post 4, not sure how you missed that. Quote Link to comment https://forums.phpfreaks.com/topic/156842-solved-please-help-with-my-array/#findComment-826224 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.