Jump to content

comparing variables in foreach loop


crazytonyi

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/131425-comparing-variables-in-foreach-loop/
Share on other sites

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;

 

 

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.