Reputation: 27
I have a website with events, I got two important tables, one with all adresses for the first Newsletter and one for the registered users.
Now I want to send a Reminder to all adresses who doesn't register.
My idea is to compare the E-Mail Adresses from both tables (tbl_adresses
and tbl_registered_usr
) and "remove" all E-Mail Adresses which are in both tables.
My code looks like:
// Read records
$query = "SELECT email FROM wf_anlaesse WHERE anlaesseID = '$Datum'";
$query = mysql_query($query);
// Put them in array
for($i = 0; $array[$i] = mysql_fetch_assoc($query); $i++) ;
// Delete last empty one
array_pop($array);
print_r ($array);
echo "<br /><br /><br /><br />";
// Read records
$query2 = "SELECT email FROM wf_adressen";
$query2 = mysql_query($query2);
// Put them in array
for($i = 0; $array2[$i] = mysql_fetch_assoc($query2); $i++) ;
// Delete last empty one
array_pop($array2);
print_r ($array2);
echo "<br /><br /><br /><br />";
$result = array_diff($array, $array2);
print_r($result);
My output is like this:
Array (
[0] => Array ( [email] => E-MAIL )
[1] => Array ( [email] => E-MAIL )
[2] => Array ( [email] => E-MAIL )
[3] => Array ( [email] => E-MAIL ) ... and so on
Array (
[0] => Array ( [email] => E-MAIL )
[1] => Array ( [email] => E-MAIL )
[2] => Array ( [email] => E-MAIL )
[3] => Array ( [email] => E-MAIL )`... and so on
Array ( )
The array_diff
doesn't do the trick.
Any ideas?
Thanks! Chris
Upvotes: 1
Views: 112
Reputation: 94
Looks to me like you want a left join, which gets all of the values in the first (left) table which match the condition. Using IS NULL
you can specify values which aren't present in the second (right) table.
SELECT A.email
FROM wf_adressen A
LEFT JOIN wf_anlaesse B
ON A.email = B.email
WHERE B.email IS NULL
This will return all the emails in wf_adressen that do not appear in wf_anlaesse.
Here's an example using PostgreSQL:
CREATE TABLE wf_anlaesse (id SERIAL, email TEXT);
CREATE TABLE wf_adressen (id SERIAL, email TEXT, name TEXT);
INSERT INTO wf_adressen VALUES (DEFAULT, '[email protected]', 'Nik M');
INSERT INTO wf_adressen VALUES (DEFAULT, '[email protected]', 'Nigel J');
INSERT INTO wf_anlaesse VALUES (DEFAULT, '[email protected]'); -- only Nik's received it
SELECT A.email
FROM wf_adressen A
LEFT JOIN wf_anlaesse B
ON A.email = B.email
WHERE B.email IS NULL;
email
-------------------
[email protected]
While MySQL may or may not be different around creating the tables and inserting the values, I'm sure the join syntax is exactly the same.
Upvotes: 1
Reputation: 4021
foreach($array as $val){
foreach($array2 as $key => $val2){
if($val["email"] == $val2["email"])
unset($array2[$key]);
}
}
Upvotes: 0
Reputation: 19
Replace
$result = array_diff($array, $array2);
with:
$result = array_intersect($array, $array2);
regards!
Upvotes: 0