Peter
Peter

Reputation: 41

Iterate through two arrays and compare results to display results that do not match

so I have an interesting problem. I have two arrays of userId and contractId that I want to compare to see if any of the results match. Then display only the ones that do not match. Any help would be appreciated.

//select all users and contracts possibilities
$rQ = "SELECT users.userId AS userId, cId AS contractId, name, cTitle FROM contracts, users WHERE users.roleId = contracts.roleId ORDER BY users.userId ASC ";
$rR = mysql_query($rQ);
$a = mysql_fetch_assoc($rR);

//selects current signatures in signatures table
$rQ1 = "SELECT userId, contractId FROM signatures ORDER BY userId ASC ";
$rR1 = mysql_query($rQ1);
$a1 = mysql_fetch_assoc($rR1);

Example: if

$a['userId'] != $a1['userId'] && $a['contractId'] != $a1['contractId']

then

echo $a['name'] . $a['cTitle'] 

Upvotes: 0

Views: 151

Answers (2)

Tucker
Tucker

Reputation: 7362

You can do the following if you need to use PHP to do it:

foreach($a as $value ){

    if(!in_array($value,$a1)){
         $value1 = array_search($value,$a1);
        if(($value['userId']!=$value1['userId']) && (($value['contractId']!=$value1['contractId']) ))
            print_r($value);

}

Upvotes: 0

Dave
Dave

Reputation: 3288

Use array_diff() http://php.net/manual/en/function.array-diff.php its handy for comparing arrays for differences and will return a list of results in array 1 that aren't in array 2 then if you run it again the other way around you'll end up with 2 arrays of items that only appear in one of your parent arrays and not the other.

You can then array merge to get one overall array of terms that aren't in either.

Upvotes: 1

Related Questions