0

这是我需要帮助的数组示例(我们称之为 $MY_ARRAY)

Array ( 
[0] => Array ( [NAME] => 1351449606472 [EMAIL] => aaa@yadda.com [ID] => 996774000001224477 ) 
[1] => Array ( [NAME] => 1351449605330 [EMAIL] => bbb@yadda.com [ID] => 996774000001224473 ) 
[2] => Array ( [NAME] => 1351449604318 [EMAIL] => ccc@yadda.com [ID] => 996774000001224469 ) 
[3] => Array ( [NAME] => 1351449603089 [EMAIL] => ddd@yadda.com [ID] => 996774000001224465 ) 
[4] => Array ( [NAME] => 1351449602046 [EMAIL] => eee@yadda.com [ID] => 996774000001224461 ) 
[5] => Array ( [NAME] => 1351449600883 [EMAIL] => fff@yadda.com [ID] => 996774000001224457 ) 
[6] => Array ( [NAME] => 1351449599840 [EMAIL] => ggg@yadda.com [ID] => 996774000001224453 )  
)

我知道如何使用 foreach 完成此操作,如下所示:(注意:已编辑以显示我实际在做什么,但以简化问题为代价)。

$aws = curl_init();
$url = "https://email.us-east-1.amazonaws.com/";
curl_setopt($aws, CURLOPT_URL, $url);
curl_setopt($aws, CURLOPT_RETURNTRANSFER, true);
//process data
foreach($MY_ARRAY as $X) 
{
    //set-up email
    $MY_ARRAY = $X['EMAIL'];
    $ID = $X['ID'];
    $SUBSCRIBER_ENCODE = rawurlencode($SUBSCRIBER);
    $HELLO = "";
    if (strlen($X['NAME']) == 0)
    {
        $HELLO = "Subscriber,<br /><br />";
    }
    else
    {
        $HELLO = $X['NAME'].",<br /><br />";
    }
    $EMAIL_BODY_ENCODE = rawurlencode($HELLO.$BODY);
    //tune curl vars
    $DATE = gmdate('D, d M Y H:i:s e');
    $HASH = hash_hmac('sha1', $DATE, $AWSPRI, true);
    $KEYS = base64_encode($HASH);
    $headers = array();
    $headers[] = "Host: email.us-east-1.amazonaws.com";
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    $headers[] = "Date: ".$DATE;
    $auth = "AWS3-HTTPS AWSAccessKeyId=".$AWSPUB;
    $auth .= ",Algorithm=HmacSHA1,Signature=".$KEYS;
    $headers[] = "X-Amzn-Authorization: ".$auth;
    $MAIL = "Action=SendEmail&Source=".$XROM_ENCODE."&ReturnPath=".$BOUNCE_ENCODE."&Destination.ToAddresses.member.1=".$SUBSCRIBER_ENCODE."&Message.Subject.Data=".$SUBJECT_ENCODE."&Message.Body.Html.Data=".$EMAIL_BODY_ENCODE;
    curl_setopt($aws, CURLOPT_POSTFIELDS, $MAIL);
    curl_setopt($aws, CURLOPT_HTTPHEADER, $headers);
    $resp = curl_exec($aws);
}
//complete log and close curl
curl_close($aws);

所以我想要的是一次通过三个数组。

4

1 回答 1

1

好的,如果我不了解需要,我认为我对问题的理解足以回答。可能有很多方法可以做到这一点。一种方法可能是

// make array into an array of little arrays
$littleArrays = array_chunk($input_array, 3);
// now we loop through outer array
for($i=0;$i<sizeof($littleArrays);$i++){
    // now we loop through the inner arrays
    for($j=0;$j<sizeof($littleArrays[$i]);$j++){
       mail($littleArrays[$i][$j]['EMAIL'], "Example Subject", "Hello ".$littleArrays[$i][$j]['name']);
    }
}

如果我理解正确的话。可能有更快或更好的方法。我认为这是一个非正统的解决方案,因为我不明白您当时为什么要发送 3 个。

请注意,这不是一个完整的解决方案......例如,如果数组长度不能被 3 整除,它会出错......

考虑到这一点,第一条评论是最好的解决方案ARRAY CHUNK - PHP.NET

编辑:解决方案更改为使用带有简单 for 循环的 array_chunk 来导航它们。

于 2012-10-29T19:24:54.930 回答