0

$message各受信者 (受信者は)をループする必要がありarray、何か問題が発生した場合はunset、対応する受信者:

$recipients = &$message->getRecipients(); // array

// Do this now as arrays is going to be altered in the loop
$count = count($recipients);         

for($i = 0; $i <= $count; $i++):

    $response = $messageManager->send($recipients[$i], $content);

    // If not 200 OK remove the recipient from the message
    if($response->getStatusCode !== 200) unset($recipients[$i]); 

endfor;

しかし、「変数のみを参照によって割り当てることができる」ため、PHP ではこれを行うことができません。配列を再割り当てする以外に、私にできることはありますか:

$recipients = $message->getRecipients();

// Loop

$message->setRecipients($recipients);

EDIT : foreach を使用して現在の要素を参照渡しすることはできません:

$recipients = array('a', 'b', 'c');

foreach($recipients as &$recipient)
    unset($recipient);

echo count($recipients); // 3
4

1 に答える 1

-1

代わりにこれを行うことができます:

$recipients = $message->getRecipients();      

foreach($recipients as $key => $recipient) :

    $response = $messageManager->send($recipient, $content);
    if($response->getStatusCode !== 200) unset($recipients[$key]); 

endforeach;

$message->setRecipients($recipients);
于 2012-08-02T15:07:36.190 に答える