Search the Community
Showing results for tags 'strings as keys'.
-
Hello, everyone. I created a multidimensional array that consists of 4 music genres in which each genre has 3 artists(using strings as keys) who in turn have 4 albums each(string values), but when I iterate over the multidimensioal array with a foreach loop, only the first album(the 1st string value) is associated with the artist(the string key). My intention was to map multiple string values to each string key/index, and have an output as such. Artist: Miles Davis Albums: Kind of Blue, Bitches Brew, Sketches of Spain, Birth of the Cool. Here's the code: <?php $jazz = array ( 'Miles Davis' => 'Kind of Blue', 'Bitches Brew', 'Sketches of Spain', 'Birth of the Cool', 'John Coltrane' => 'A Love Supreme', 'Blue Train', 'Giant Steps', 'My Favorite Things', 'Dave Brubeck' => 'Time Out', 'Time Further Out', 'Times Changes', 'Indian Summer', ); $classic_rock = array ( 'The Beatles' => 'Abbey Road', 'Sgt. Pepper\'s Lonely Hearts Club', 'Revolver', 'Rubber Soul', 'Pink Floyd' => 'The Dark Side of The Moon', 'The Wall', 'Wish You Were Here', 'Meddle', 'The Doors' => 'Doors', 'Strange Days', 'L.A Woman', 'Morrison Hotel', ); $alternative_rock = array ( 'Pearl Jam' => 'Ten', 'Vs.', 'Yield', 'No Code', 'Radiohead' => 'Pablo Honey', 'Kid A', 'O.K Computer', 'In Rainbows', 'Dave Matthews Band' => 'Under the Table and Dreaming', 'Busted Stuff', 'Crash', 'Before These Crowded Streets', ); $indie_rock = array ( 'The National' => 'Boxer', 'High Violet', 'Sad Songs for Dirty Lovers', 'Cherry Tree', 'Interpol' => 'Turn On The Bright Lights', 'Antics', 'Interpol', 'Our Love To Admire', 'The Radio Dept.' => 'Pet Grief', 'Clinging To A Scheme', 'Lesser Matters', 'Passive Aggressive', ); $music = array ( 'Jazz' => $jazz, 'Classic Rock' => $classic_rock, 'Alternativ Rock' => $alternative_rock, 'Indie Rock' => $indie_rock, ); foreach ($music as $genre => $artists) { print "<p>Genre: $genre"; foreach ($artists as $artist => $albums) { print "<br />" . "Artist: " . $artist . "\t" . "Albums: " . "$albums"; } print '</p>'; } ?> Here's the output: Genre: Jazz Artist: Miles Davis Album: Kind of Blue Artist: 0 Album: Bitches Brew Artist: 1 Album: Sketches of Spain Artist: 2 Album: Birth of the Cool Artist: John Coltrane Album: A Love Supreme Artist: 3 Album: Blue Train Artist: 4 Album: Giant Steps Artist: 5 Album: My Favorite Things Artist: Dave Brubeck Album: Time Out Artist: 6 Album: Time Further Out Artist: 7 Album: Times Changes Artist: 8 Album: Indian Summer Genre: Classic Rock Artist: The Beatles Album: Abbey Road Artist: 0 Album: Sgt. Pepper's Lonely Hearts Club Artist: 1 Album: Revolver Artist: 2 Album: Rubber Soul Artist: Pink Floyd Album: The Dark Side of The Moon Artist: 3 Album: The Wall Artist: 4 Album: Wish You Were Here Artist: 5 Album: Meddle Artist: The Doors Album: Doors Artist: 6 Album: Strange Days Artist: 7 Album: L.A Woman Artist: 8 Album: Morrison Hotel Genre: Alternativ Rock Artist: Pearl Jam Album: Ten Artist: 0 Album: Vs. Artist: 1 Album: Yield Artist: 2 Album: No Code Artist: Radiohead Album: Pablo Honey Artist: 3 Album: Kid A Artist: 4 Album: O.K Computer Artist: 5 Album: In Rainbows Artist: Dave Matthews Band Album: Under the Table and Dreaming Artist: 6 Album: Busted Stuff Artist: 7 Album: Crash Artist: 8 Album: Before These Crowded Streets Genre: Indie Rock Artist: The National Album: Boxer Artist: 0 Album: High Violet Artist: 1 Album: Sad Songs for Dirty Lovers Artist: 2 Album: Cherry Tree Artist: Interpol Album: Turn On The Bright Lights Artist: 3 Album: Antics Artist: 4 Album: Interpol Artist: 5 Album: Our Love To Admire Artist: The Radio Dept. Album: Pet Grief Artist: 6 Album: Clinging To A Scheme Artist: 7 Album: Lesser Matters Artist: 8 Album: Passive Aggressive Any help is appreciated, but what I'd really like to understand is what I'm doing wrong/ or missing here. Thanx.
- 10 replies
-
- multidimensional
- array
-
(and 1 more)
Tagged with: