Edward Posted May 18, 2006 Share Posted May 18, 2006 Here is the code I am using:$members = 'Person1, Person2';$array = explode(',',$members); //this creates an array by exploding your addresses at the character ','foreach ($array AS $person) { if ($person == 'Person1') { $number = 'Odd'; } if ($person == 'Person2') { $number = 'Even'; } $body = 'Hi there ' . $person . ', you are ' . $number; echo $body;}Here is the desired outcome:Hi there Person1, you are OddHi there Person2, you are EvenHere is the ACTUAL outcome:Hi there Person1, you are OddHi there Person2, you are OddCan anyone see what I'm doing wrong? I'm trying to create a basic code I can use to specify one variable depending on what aother is, inside a loop. Link to comment https://forums.phpfreaks.com/topic/9957-basic-array-problems/ Share on other sites More sharing options...
AndyB Posted May 18, 2006 Share Posted May 18, 2006 I can't see the difference between the actual outcome and the desired outcome Maybe we need a different example?? Link to comment https://forums.phpfreaks.com/topic/9957-basic-array-problems/#findComment-37020 Share on other sites More sharing options...
Edward Posted May 18, 2006 Author Share Posted May 18, 2006 [!--quoteo(post=375080:date=May 18 2006, 11:49 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ May 18 2006, 11:49 PM) [snapback]375080[/snapback][/div][div class=\'quotemain\'][!--quotec--]I can't see the difference between the actual outcome and the desired outcome Maybe we need a different example??[/quote]Sorry, I'm tired, I've amended it now.Here is the ACTUAL outcome:Hi there Person1, you are OddHi there Person2, you are Odd Link to comment https://forums.phpfreaks.com/topic/9957-basic-array-problems/#findComment-37021 Share on other sites More sharing options...
kenrbnsn Posted May 19, 2006 Share Posted May 19, 2006 Those darn pesky spaces ...You wrote:[code]<?php$members = 'Person1, Person2';$array = explode(',',$members); //this creates an array by exploding your addresses at the character ','foreach ($array AS $person) {if ($person == 'Person1') { $number = 'Odd'; }if ($person == 'Person2') { $number = 'Even'; }$body = 'Hi there ' . $person . ', you are ' . $number;echo $body;}?>[/code]Notice you have a space after the comma, so the second member of the array is really ' Person2'. That's why the "if" fails. Remove the space or use:[code]<?phpif (trim($person) == 'Person1') { $number = 'Odd'; }if (trim($person) == 'Person2') { $number = 'Even'; }?>[/code]for your comparison statements.Ken Link to comment https://forums.phpfreaks.com/topic/9957-basic-array-problems/#findComment-37034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.