m.jay.victor Posted October 20, 2010 Share Posted October 20, 2010 <html> <head></head> <body> My favourite bands are: <ul> <?php // define arrays $morebands = array('Desturbed', 'Anthrax'); $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands"); // loop over it // print array elements foreach ($artists as $a) { if ($a != 'Array'){ echo '<li>'.$a; } Else { foreach ("${$a}" as $b){ echo '<li>'.$b; } } } ?> </ul> </body> </html> I can not figure out why this will not work:( I would like the foreach to run through the array as normal, but if it encounters a nested array, loop it as well. I know this likely is not the right, or best way to do this, but I am just learning PHP through a tutorial and I learn best by doing... So I take the lessons, make them more complicated, then figure out how to make it happen (like so). right now I am working on http://devzone.zend.com/node/view/id/635 anyhow thanks for any help! Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 20, 2010 Share Posted October 20, 2010 Try the following... <html> <head></head> <body> My favourite bands are: <ul> <?php // define arrays $morebands = array('Desturbed', 'Anthrax'); $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands); // echo each value foreach($artists AS $a) { if(gettype($a) == 'array') { foreach($a AS $b) { echo '<li>',$b; } } else { echo '<li>',$a; } } ?> </ul> </body> </html> Tell me how it goes bud Regards, Paul. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 I'm wondering why you're putting the $morebands array as an array inside of the $artists array. This seems more logical: $morebands = array('Desturbed', 'Anthrax'); $artists = array_merge(array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses'), $morebands); foreach ($artists as $a) { echo "<li>$a</li>"; } Quote Link to comment Share on other sites More sharing options...
m.jay.victor Posted October 20, 2010 Author Share Posted October 20, 2010 to learn how. I started learning PHP about 2 days ago and I am following that tutorial I linked earlier. The next logical step for me was to see if I could nest the arrays. Anyhow, I noticed part of what you fixed was the way I defined the artist array. from $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands"); to $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands); What i don't understand is why this was necessary. I know it was, because the code would output the word "array" on the last line if you did not remove the quotes. Can you explain why this happened though? Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted October 20, 2010 Share Posted October 20, 2010 If I'm not mistaken, I'm sure quoting an array makes it a string and not an actual array. Although I am not 100% certain on this, I'll look into it and post back Regards, Paul. Quote Link to comment Share on other sites More sharing options...
m.jay.victor Posted October 20, 2010 Author Share Posted October 20, 2010 thanks again... any advice for a beginner? Good tutorial sites, text editors etc? I downloaded zendstudio IDE trial but that seems like I am bringing a nuclear weapon to a knife fight. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 20, 2010 Share Posted October 20, 2010 to learn how. I started learning PHP about 2 days ago and I am following that tutorial I linked earlier. The next logical step for me was to see if I could nest the arrays. Anyhow, I noticed part of what you fixed was the way I defined the artist array. from $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', "$morebands"); to $artists = array('Metallica', 'Evanescence', 'Linkin Park', 'Guns n Roses', $morebands); What i don't understand is why this was necessary. I know it was, because the code would output the word "array" on the last line if you did not remove the quotes. Can you explain why this happened though? Look more closely. I used array_merge() to merge the two arrays. Your way adds the $morebands array as a nested array into another element in the $artists array and looks like this: Array ( [0] => Metallica [1] => Evanescence [2] => Linkin Park [3] => Guns n Roses [4] => Array ( [0] => Desturbed [1] => Anthrax ) ) Mine looks like this: Array ( [0] => Metallica [1] => Evanescence [2] => Linkin Park [3] => Guns n Roses [4] => Desturbed [5] => Anthrax ) Quote Link to comment Share on other sites More sharing options...
m.jay.victor Posted October 21, 2010 Author Share Posted October 21, 2010 Thank you Abra, I was however trying to extend my knowledge of foreach, and how I may nest arrays. Your way is by far functionally better of course, but I am just trying to extend what I got from that one lesson. You did however teach me that it was possible to merge arrays though:) Wich brings me to... $artists = array_merge($artists, $morebands); Would that have not been simpler? Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 21, 2010 Share Posted October 21, 2010 Thank you Abra, I was however trying to extend my knowledge of foreach, and how I may nest arrays. Your way is by far functionally better of course, but I am just trying to extend what I got from that one lesson. You did however teach me that it was possible to merge arrays though:) Wich brings me to... $artists = array_merge($artists, $morebands); Would that have not been simpler? Oh O.K. In that case you would use something like what PaulRyan posted. I would however use in_array($a) instead of the gettype(). Quote Link to comment 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.