Ste
Ste

Reputation: 1520

Php print array help

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!

Upvotes: 1

Views: 184

Answers (4)

sdolgy
sdolgy

Reputation: 7001

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;

Upvotes: 0

Jan Turoň
Jan Turoň

Reputation: 32912

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

Upvotes: 1

Andrew Moore
Andrew Moore

Reputation: 95334

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);
}

Upvotes: 0

Alex Pliutau
Alex Pliutau

Reputation: 21957

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.

Upvotes: 0

Related Questions