Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何指示PHP数组中的最后一个标记?_Php_Arrays_Email_Indices - Fatal编程技术网

如何指示PHP数组中的最后一个标记?

如何指示PHP数组中的最后一个标记?,php,arrays,email,indices,Php,Arrays,Email,Indices,我有一个简单的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; els

我有一个简单的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";
?>

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

无需

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

php包括
内爆
连接
,在这里工作得更好:

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

只需使if子句使用以下函数:

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

循环中的字符串浓缩。杀死me@ColeJohnson我可以帮你-你现在在哪里?:)@科尔:有两种愚蠢:1。不知道;2.嘲笑那些不知道的人。后者要大得多。@alfasin南加州好吗?@ColeJohnson它是SF,答案是“地狱耶”;)这直接回答了我的问题,我们对此表示赞赏,其他重构方面也是如此value@khanahk:除非它没有<代码>数组(1,2,4,2)