running_out_of_imagination Posted February 27, 2006 Share Posted February 27, 2006 I have this foreach statement that is supposed to take data from an array and process it to put it an e-mail, but I've noticed that the script does it as many times as your user id. Can anyone figure out why?Array Structure:Array( [1] => Array ( [name] => John Doe [email] => john@doe.com [6] => Array ( [start] => 1141124400 [end] => 1141131600 [name] => Training [desc] => We'll have training today ) [7] => Array ( [start] => 1141207200 [end] => 1141221600 [name] => Party [desc] => Party on high street ) ))PHP code:[code]foreach($events as $m => $id){ //per player foreach($id as $j => $k){if(($j != "name")&&($j != "email")){ $text.=$k['name']." is on ".date("l dS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']).".".$k['desc'].".";}}} [/code]Does anyone know how to fix this? Quote Link to comment Share on other sites More sharing options...
Barand Posted February 27, 2006 Share Posted February 27, 2006 Exactly what output are you expecting from the above? I get[code]Training is on Tuesday 28th of February from 11:00 till 13:00We'll have training todayParty is on Wednesday 1st of March from 10:00 till 14:00Party on high street[/code]BTW, you will find a TAB key on the left end of your keyboard, 4 rows up. It does help others to read your code.[code]$events = array( '1' => array ( 'name' => 'John Doe', 'email' => 'john@doe.com', '6' => array ( 'start' => 1141124400, 'end' => 1141131600, 'name' => 'Training', 'desc' => "We'll have training today" ), '7' => array ( 'start' => 1141207200, 'end' => 1141221600, 'name' => 'Party', 'desc' => 'Party on high street' ) ));foreach($events as $m => $id){ //per player foreach($id as $j => $k){ if(($j != "name")&&($j != "email")){ $text.= "{$k['name']} is on ".date("l jS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']). "<br>{$k['desc']}<br><br>"; } }}echo $text;[/code] Quote Link to comment Share on other sites More sharing options...
running_out_of_imagination Posted February 28, 2006 Author Share Posted February 28, 2006 Hey,the outcome is right and sorry for the tab. But php repeats this as many times as the user's id. So if my user id is 3 I'll get:[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Training is on Tuesday 28th of February from 11:00 till 13:00We'll have training todayParty is on Wednesday 1st of March from 10:00 till 14:00Party on high streetTraining is on Tuesday 28th of February from 11:00 till 13:00We'll have training todayParty is on Wednesday 1st of March from 10:00 till 14:00Party on high streetTraining is on Tuesday 28th of February from 11:00 till 13:00We'll have training todayParty is on Wednesday 1st of March from 10:00 till 14:00Party on high street[/quote]Is it because I have a foreach in another foreach or is some variable in the wrong place? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 1, 2006 Share Posted March 1, 2006 Is the code you posted inside another loop? Quote Link to comment Share on other sites More sharing options...
running_out_of_imagination Posted March 1, 2006 Author Share Posted March 1, 2006 [!--quoteo(post=350439:date=Mar 1 2006, 12:02 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 1 2006, 12:02 AM) [snapback]350439[/snapback][/div][div class=\'quotemain\'][!--quotec--]Is the code you posted inside another loop?it'[/quote]No, it's inside the two foreach loops:[code]foreach($events as $m => $id){ //per player foreach($id as $j => $k){ if(($j != "name")&&($j != "email")){ $text.=$k['name']." is on ".date("l dS of F \f\\r\o\m H:i",$k['start'])." till ".date("H:i",$k['end']).". ".$k['desc'].". "; }}} [/code] 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.