php - 简单数组 foreach

标签 php

我敢肯定这很简单......但我似乎无法弄明白。我需要向每个提及要点的电子邮件地址发送一封电子邮件。但是两封邮件都有36分。

$emails = $userTools->weeklyMail();

打印_r:

Array
(
    [0] => Array
        (
            [0] => email1@gmail.com
            [1] => 
            [2] => 36
        )

    [1] => Array
        (
            [0] => email2@gmail.com
            [1] => 
            [2] => 25
        )

)

循环:

foreach($emails as $email)
{
        $email = $email[0];
        $subject = "You have ".$email[2]." points!!! !!!";
        // The message
        $message = "Hello\r\nYou have ".$email[2]." points .";
        $helperClass->sendEmail($email, $subject, $message);
}

最佳答案

您的问题是您覆盖了引用变量 $email,这意味着 $email[2] 未定义。

更改为:

foreach($emails as $email)
{
        // $email = $email[0]; You can't use $email[2] if $email is overwritten
        $subject = "You have ".$email[2]." points!!! !!!";
        // The message
        $message = "Hello\r\nYou have ".$email[2]." points .";
        $helperClass->sendEmail($email[0], $subject, $message);
}

关于php - 简单数组 foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31049957/

相关文章:

php - 从 PHP 一个简单的 Linux 脚本返回 "Hello World"

PHP Composer 包含定义了 "use namespace as"语句的文件,附加到每个文件

php - 从 php json 文件中检索名称 ID

php - html/blog中的代码效果

php - 从字符串是可变的字符串中提取字符串

php - 在 WooCommerce 中设置优惠券描述

php - 网页编程原则

php - Laravel 4 未找到类和命名空间

javascript - 将多个 PHP 文件的请求合并为一个请求

php - 使用 mysql 资源在循环中做一个循环