1

I have this array

Array ( 
  [3] => Array ( 
     [IDFattura] => 3 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 343.30 
     [IDTipo_Offerta] => A 
     [Email] => [email protected] ) 
  [4] => Array ( 
     [IDFattura] => 4 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 98.40 
     [IDTipo_Offerta] => A 
     [Email] => [email protected] )
  [7] => Array ( 
     [IDFattura] => 33 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 18.40 
     [IDTipo_Offerta] => A 
     [Email] => [email protected] ) )  

Now I need send ONE email to each Email, but [email protected] (in email body ) will have a table with two rows, instead of Tom that will have 1 row. Hope you understand me!

1

4 Answers 4

1

try this code

$newarray = array();
foreach($array as $item) $newarray[$item["Email"]] = 1;
$sendarray = array_keys($newarray);
foreach($sendarray as $item) mail(...);

you should also consider array_unique

good luck

Sign up to request clarification or add additional context in comments.

Comments

0

You should reformat your array like this:

$newArray = array();
foreach ($yourArray as $key => $value) {
   $newArray[$value['Email']][] = $value;
}

It returns array grouped by Email. And for [email protected] tou will have an array with 2 items.

Comments

0

Loop through the array and group invoices by email:

$invoicesByEmail = array();

foreach($invoices as $invoice) {
  if(!isset($invoicesByEmail[$invoice['Email']])) {
    $invoicesByEmail[$invoice['Email']] = array();
  }

  $invoicesByEmail[$invoice['Email']][] = $invoice;
}

Then, it's a matter of looping through the grouped invoice and mailing them.

foreach($invoicesByEmail as $recipient => $invoices) {
  $emailBody = '';

  foreach($invoices as $invoice) {
    // Parse your invoice
  }

  Mailer::send($recipient, $emailBody, $headers);
}

Comments

0

Personally, I'd structure the array slightly different. Instead of having numeric keys, i'd set the key as the email address. This way you can simply use array_unique.

If you can't change the array as you get it now, you can loop through it and extract each email address out and insert it into a new array:

$uniqueEmails = array();
foreach ($yourArray as $k => $v) { 
  if (isset($v['Email']) $uniqueEmails[$v['Email']] = $v['Email'];
}
return $uniqueEmails;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.