Jump to content

Is this right


Recommended Posts

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] => [email protected]
[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?
Link to comment
https://forums.phpfreaks.com/topic/3696-is-this-right/
Share on other sites

Exactly what output are you expecting from the above? I get

[code]Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party 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' => '[email protected]',
            '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]

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:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street

Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street

Training is on Tuesday 28th of February from 11:00 till 13:00
We'll have training today

Party is on Wednesday 1st of March from 10:00 till 14:00
Party on high street[/quote]

Is it because I have a foreach in another foreach or is some variable in the wrong place?
[!--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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.