Removing duplicarte email addresses from existing list

4 posts by 2 authors in: Forums > CMS Builder
Last Post: December 21, 2012   (RSS)

I’ve been mucking about trying to remove duplicate entries from an existing email list and although I got this to work, it’s really dependent on the lack of spaces spaces in the foreach loop code.

I’m thinking that there must be an easier way to insure that this works.

Here’s the code that I’m using:

<?php ob_start(); // start capturing output ?>
<?php foreach($client_uploadsRecords as $record) : ?><?php foreach ($record['uploads'] as $upload): ?><?php if($upload['info5'] == 0 || !$upload['info5']) : ?><?php $email = strtolower($record['email']) ; ?>
<?php // Replace all errant spaces in $email field with nothing
$email = preg_replace("/[ ]/", "", $email); ?><?php echo $email ?>;<?php endif; ?><?php endforeach ?><?php endforeach ?>
<?php $output = ob_get_clean(); // stop capturing output ?>
<?php // Replace all spaces in $output with nothing
$object = preg_replace("/[ ]/", "", $object); ?>
<?php $output = implode('; ',array_unique(explode(';', $output))); ?>
<?php echo $output ?>


Thanks for the help,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Hi Jerry,

Is there a particular reason that your using output buffering to record the e-mail addresses into the array? I've modified your code so that it stores the e-mail addresses directly into an array:

<?php
$object = array();
foreach($client_uploadsRecords as $record){
foreach ($record['uploads'] as $upload{
if($upload['info5'] == 0 || !$upload['info5']){
//change e-mail address to lowercase.
$email = strtolower($record['email']) ;
//clean e-mail address
$email = preg_replace("/[ ]/", "", $email);
//If the e-mail address isn't already in the array, add it too it.
if(!in_array($email,$object)){
$object[] = $email;
}
}
}
}
//implode the output array.
$final_object = implode('; ', $output);
//... and then display it!
echo $final_object;
?>


So in this example I'm storing the e-mail address directly into the array, but only if the address isn't already in there.

Let me know if this doesn't work, or if you need something different.

Thanks!

Greg
Greg Thomas







PHP Programmer - interactivetools.com

By gkornbluth - December 21, 2012 - edited: December 21, 2012

Hi Greg,

After a few minor tweeks, like adding a close parenthesis after foreach ($record['uploads'] as $upload and changing $ouput to $object in final_object = implode('; ', $output); it worked like a charm with no more space issues.

Final code is below.

Thanks again,

BTW, Thanks for annotating the code so that I could understansd what was going on.

Jerry Kornbluth

$object = array();
foreach($client_uploadsRecords as $record){
foreach ($record['uploads'] as $upload){
if($upload['info5'] == 0 || !$upload['info5'])
{
//change e-mail address to lowercase.
$email = strtolower($record['email']) ;
//clean e-mail address
$email = preg_replace("/[ ]/", "", $email);
//If the e-mail address isn't already in the array, add it too it.
if(!in_array($email,$object)){
$object[] = $email;
}
}
}
}
//implode the output array.
$final_object = implode('; ', $object);
//... and then display it!
echo $final_object;
?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php