2017-08-01 44 views
-1

我有一個下面的foreach功能:獲取所有數組值然後將其設置爲的foreach函數在PHP

foreach ($agg as $dep => $emails) 
{ 
    $result[] = [ 
     'DEPARTMENTID_FK' => $dep, 
     'to' => implode(',', $emails), 
    ]; 
} 

echo $to = $result[0]['to']; 

$host = 'smtp.office365.com'; 
    $port = '587'; 
    $username = '[email protected]; 
    $password = 'sw0052?'; 

    $headers = array(
    'Port'   => $port, 
    'From'   => $from, 
    'To'   => $to, 
    'Subject'  => $subject, 
    'Content-Type' => 'text/html; charset=UTF-8' 
    ); 

    $recipients = $to.", ".$bcc; 

    $smtp = Mail::factory('smtp', 
    array ('host' => $host, 
    'auth' => true, 
    'username' => $username, 
    'password' => $password)); 

    $mail = $smtp->send($recipients, $headers, $body); 

    if (PEAR::isError($mail)) { 
     echo("<p>" . $mail->getMessage() . "</p>"); 
    } else { 
     echo("<p>Message successfully sent!</p>"); 
    } 

現在,我怎麼能得到的所有$to的價值?

現在我需要手動編輯這個$result[0]['to']$result[1]['to']看到另一個數據。

我想讓它在循環中。可能嗎?

因此,如果有3個數據,它將在foreach上運行另一個查詢3次。

使用print_r的

Array ([0] => Array ([DEPARTMENTID_FK] => DP0023 [to] => [email protected],[email protected]) [1] => Array ([DEPARTMENTID_FK] => DP0026 [to] => [email protected])) 
+0

你可以var轉儲數組,所以我們可以看到它看起來像什麼?我有點困惑爲什麼你嘗試$結果** [0] ** ['到'] – Andreas

+0

嗨@Andreas請參閱我的更新後 –

+0

這是$ agg數組?你想做什麼? – Andreas

回答

2
// version 1 
foreach ($result as $item) { 
    echo $item['to']; 
} 


// version 2 
$dep_id = '1'; 
foreach ($result as $item) { 
    if ($dep_id == $item['DEPARTMENTID_FK']) { 
     echo $item['to']; 
     break; 
    } 
} 

// version 3 
foreach ($agg as $dep => $emails) 
{ 
    $result[$dep] = [ 
     'DEPARTMENTID_FK' => $dep, 
     'to' => implode(',', $emails), 
    ]; 
}  

$dep_id = '7'; 
echo $result[$dep_id]['to']; 

// version 4 
foreach ($result as $item) { 
    // more code here 

    $recipients = $item['to'].", ".$bcc; 
    $mail = $smtp->send($recipients, $headers, $body); 

    // more code here 
} 
+0

其實我已經根據部門劃分了這個電子郵件地址。如果我使用你的代碼,所有電子郵件都會顯示。我需要的是根據部門運行的代碼 –

+0

查看更新。 –

+0

感謝您的更新,但是我的意思是,您可以在print_r上看到DP0023有一個電子郵件地址:[email protected],[email protected]和DP0026有一個電子郵件地址:david.alwis @ siix.com.sg.我需要在那裏設置電子郵件功能,所以電子郵件功能會向2個部門發送2次。 –

1

使用implode()

電子郵件ID已經存在。

你只需要把它們和implode()變成刺。

$to = implode($agg); 
0

使用變量來保存你的循環to字符串echo它:

foreach ($agg as $dep => $emails) 
{ 
    $to = implode(',', $emails); 
    $result[] = [ 
     'DEPARTMENTID_FK' => $dep, 
     'to' => $to, 
    ]; 
    echo $to; // print it 
} 

更新

$toList = []; 
foreach ($agg as $dep => $emails) { 
    $to = implode(',', $emails); 
    $result[] = [ 
     'DEPARTMENTID_FK' => $dep, 
     'to' => $to, 
    ]; 

    $toList[] = $to; 
} 

$toString = implode(',', $toList); 
+0

嗨Jared,請看我的問題發佈更新。 –

+0

我更新了它,嘗試打印'$ toString' –

+0

您可以看到DP0023 =「[email protected],[email protected]」和DP0026 =「[email protected]」I只想發送基於2部門的電子郵件。如果我剛剛使用您的代碼,它將只發送一次到所有電子郵件列表。 –

0

我想我現在你正在嘗試理解做。
嘗試一下;
它將遍歷每個部門並收集所有電子郵件。

foreach ($agg as $dep) 
{ 
    $temparr[] = dep['to']; 
} 
$youremails = implode(",", $temparr); 

echo $youremails; // comma separated list. 
相關問題