aquastream Posted February 7, 2010 Share Posted February 7, 2010 Here's another newbie question which I'm confused about. I've used the foreach function to list the contents of the first array. However, on the second array I can't get the same results. I simply get the last result echoed and the other ones are ignored. <?php $array1 = array("Lisa" => 28,"Jack" => 16,"Ryan" => 35,"Rachel" => 46,"Grace" => 34); foreach($array1 as $names => $numbers) echo "Name: $names, Age: $numbers <br />" ?> <br /> <?php $beatles = array("Singer 1" => "Paul","Singer 2" => "John","Guitarist" => "George","Drummer" => "Ringo"); foreach($beatles as $roles => $names); echo "$roles: $names <br />" ?> Can anyone see what I've done wrong here? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/ Share on other sites More sharing options...
shadiadiph Posted February 7, 2010 Share Posted February 7, 2010 $names => $numbers $roles => $names i think you should rename $names in the second one maybe confusing it as $names from the first foreach call or maybe its the ; after foreach($beatles as $roles => $names); or foreach ($beatles as $roles => $names) { echo '$roles: $names <br />'; } sorry i am very tired but try this. <?php $array1= array(); $array1 = array("Lisa" => 28,"Jack" => 16,"Ryan" => 35,"Rachel" => 46,"Grace" => 34); foreach($array1 as $names => $numbers) { echo "Name: $names, Age: $numbers <br />"; } ?> <br /> <?php $beatles = array(); $beatles = array("Singer 1" => "Paul","Singer 2" => "John","Guitarist" => "George","Drummer" => "Ringo"); foreach($beatles as $roles => $names){ echo "$roles: $names <br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/#findComment-1008205 Share on other sites More sharing options...
shadiadiph Posted February 7, 2010 Share Posted February 7, 2010 also your arrays should be like this $array1 = array(); $array1= array();$array1 = array('Lisa' => '28','Jack' => '16','Ryan' => '35','Rachel' => '46','Grace' => '34'); $beatles = array(); $beatles = array('Singer 1' => 'Paul','Singer 2' => 'John','Guitarist' => 'George','Drummer' => 'Ringo'); Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/#findComment-1008207 Share on other sites More sharing options...
MadTechie Posted February 7, 2010 Share Posted February 7, 2010 Their is nothing wrong with the array or the loop (well kinda) the problem is you are terminating the loop without doing anything, So you are doing the loop without any output then displaying the last entry.. foreach($beatles as $roles => $names); should be (Note the semi-colon at the end) foreach($beatles as $roles => $names) Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/#findComment-1008209 Share on other sites More sharing options...
aquastream Posted February 7, 2010 Author Share Posted February 7, 2010 sorry i am very tired but try this. Well your code worked, even though you were tired. Thanks for that Shadiadiph! Also thanks to MadTechie for the additional clarification about terminating the loop. Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/#findComment-1008248 Share on other sites More sharing options...
shadiadiph Posted February 8, 2010 Share Posted February 8, 2010 no problem you are welcome Quote Link to comment https://forums.phpfreaks.com/topic/191216-inconsistencies-in-foreach-working/#findComment-1008605 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.