crazytonyi Posted November 5, 2008 Share Posted November 5, 2008 I thought that I just didn't get the syntax, but now I think I don't get objects at all. Please keep in mind, when offering help, that I'm using a function that creates all of my objects because I'm still getting my mind around the very idea of classes and objects.... So imagine I have an xml file that is a series of jobs. Each job is assigned to a person, but instead of grouping the xml by person, the names are put in each job. So I want to output only the first instance of each name in my loop. This is not working at all. Take a look: /*nabbed from php.net */ function bool2str($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } function compareObjects(&$o1, &$o2) { echo $o1.' == '.$o2.' : ' . bool2str($o1 == $o2) . "\n"; echo $o1.' != '.$o2.' : ' . bool2str($o1 != $o2) . "\n"; echo $o1.' === '.$o2.' : ' . bool2str($o1 === $o2) . "\n"; echo $o1.' !== '.$o2.' : ' . bool2str($o1 !== $o2) . "\n"; } /* End of nabbed code */ $xml = simplexml_load_file("somefile.xml"); foreach ($xml->object as $value) { $some_name = $value->name; //function copied directly from: http://us2.php.net/manual/en/language.oop5.object-comparison.php compareObjects($con_name, $prev_name); $prev_name = $some_name; } The compareObjects function is at php.net and seems to work as it's supposed to. Here is the output: Joe Smith == Joe Smith : FALSE Joe Smith != Joe Smith : TRUE Joe Smith === Joe Smith : FALSE Joe Smith !== Joe Smith : TRUE Doug Liszt == Joe Smith : FALSE Doug Liszt != Joe Smith : TRUE Doug Liszt === Joe Smith : FALSE Doug Liszt !== Joe Smith : TRUE Doug Liszt == Doug Liszt : FALSE Doug Liszt != Doug Liszt : TRUE Doug Liszt === Doug Liszt : FALSE Doug Liszt !== Doug Liszt : TRUE Obviously what I want is for the same value to return "True" so that I can say something like: if ($some_name !== $prev_name) echo $some_name; What am I getting wrong? Obviously I'm missing a fundamental principle. Thanks for helping! A Quote Link to comment https://forums.phpfreaks.com/topic/131425-comparing-variables-in-foreach-loop/ Share on other sites More sharing options...
DarkWater Posted November 5, 2008 Share Posted November 5, 2008 I don't see you setting $con_name anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/131425-comparing-variables-in-foreach-loop/#findComment-682611 Share on other sites More sharing options...
crazytonyi Posted November 5, 2008 Author Share Posted November 5, 2008 Good catch. Here is what it should be: /*nabbed from php.net */ function bool2str($bool) { if ($bool === false) { return 'FALSE'; } else { return 'TRUE'; } } function compareObjects(&$o1, &$o2) { echo $o1.' == '.$o2.' : ' . bool2str($o1 == $o2) . "\n"; echo $o1.' != '.$o2.' : ' . bool2str($o1 != $o2) . "\n"; echo $o1.' === '.$o2.' : ' . bool2str($o1 === $o2) . "\n"; echo $o1.' !== '.$o2.' : ' . bool2str($o1 !== $o2) . "\n"; } /* End of nabbed code */ $xml = simplexml_load_file("somefile.xml"); foreach ($xml->object as $value) { $some_name = $value->name; //function copied directly from: http://us2.php.net/manual/en/language.oop5.object-comparison.php compareObjects($some_name, $prev_name); $prev_name = $some_name; } Here is the output: Joe Smith == Joe Smith : FALSE Joe Smith != Joe Smith : TRUE Joe Smith === Joe Smith : FALSE Joe Smith !== Joe Smith : TRUE Doug Liszt == Joe Smith : FALSE Doug Liszt != Joe Smith : TRUE Doug Liszt === Joe Smith : FALSE Doug Liszt !== Joe Smith : TRUE Doug Liszt == Doug Liszt : FALSE Doug Liszt != Doug Liszt : TRUE Doug Liszt === Doug Liszt : FALSE Doug Liszt !== Doug Liszt : TRUE Obviously what I want is for the same value to return "True" so that I can say something like: if ($some_name !== $prev_name) echo $some_name; Quote Link to comment https://forums.phpfreaks.com/topic/131425-comparing-variables-in-foreach-loop/#findComment-682945 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.