如何在PHP数组中指出最后的指标?

时间:2021-12-29 08:06:26

I have a simple mail() script that sends to multiple emails dynamically by seperating the emails with commas.

我有一个简单的mail()脚本,通过用逗号分隔电子邮件动态发送到多个电子邮件。

<?php

$sendto = array("email@gmail.com", "email@zoho.com", "email@mail.usf.edu");
foreach ($sendto as $email)
{
    if ($email is the last indice in the array..) // <-- here
        $to = $email;
    else
        $to = $email . ', ';
}

$subject = "test message";
$msg = "this is a test";

if (mail($to, $subject, $msg)) 
    echo "message sent";
else 
    echo "uh oh";
?>

How do I identify if (this is the last piece of data in my array) ?

如何识别(这是我的数组中的最后一块数据)?

3 个解决方案

#1


3  

php includes implode or join which will work much better here:

php包括implode或join,这将在这里工作得更好:

$to = implode(', ', $sendto);

#2


5  

No need.

没必要。

$to = implode(', ', $sendto);

#3


1  

Just make your if clause use the end function:

只需使用if子句使用end函数:

if ($email == end($sendto))

#1


3  

php includes implode or join which will work much better here:

php包括implode或join,这将在这里工作得更好:

$to = implode(', ', $sendto);

#2


5  

No need.

没必要。

$to = implode(', ', $sendto);

#3


1  

Just make your if clause use the end function:

只需使用if子句使用end函数:

if ($email == end($sendto))