开发者

Php print array help

I have this array

Array ( 
  [3] => Array ( 
     [IDFattura] => 3 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 343.30 
     [IDTipo_Offerta] => A 开发者_JS百科
     [Email] => stefano@email.it ) 
  [4] => Array ( 
     [IDFattura] => 4 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 98.40 
     [IDTipo_Offerta] => A 
     [Email] => stefano@email.it )
  [7] => Array ( 
     [IDFattura] => 33 
     [Data_Scadenza] => 2011-06-23 
     [Importo] => 18.40 
     [IDTipo_Offerta] => A 
     [Email] => tom@email.it ) )  

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


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


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 stefano@email.it tou will have an array with 2 items.


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜