PHP数组在循环中保留旧值

时间:2017-04-30 19:32:48

标签: php arrays

我有一个php循环,用于查询数据库并向一群人发送电子邮件。在此示例中,它返回两行。

问题是它包含第二次运行中第一次运行的结果。

我的代码:

 $query = mysql_query("SELECT * FROM tasks WHERE DATE(`show_date`) = (      CURDATE() - INTERVAL 1 DAY ) AND drive_folder_empty = 'empty' AND drive_folder IS NOT NULL;");
    while ($row = mysql_fetch_assoc($query)){

        if (!empty($row['robert'])){$robert_email = "robert email"; $robert_phone = "robert phone";}
        if (!empty($row['duncan'])){$duncan_email = "duncan email"; $duncan_phone = "duncan phone";}
        if (!empty($row['mike'])){$mike_email = "mike email"; $mike_phone = "mike phone";}
        if (!empty($row['james'])){$james_email = "james email"; $james_phone = "james phone";}


        $email_array = array($robert_email, $duncan_email, $mike_email, $james_email);              
        $filtered_email = array_filter($email_array);

        print_r($filtered_email);

        $mail_to = implode(', ', $filtered_email);

        $phone_array = array($robert_phone, $duncan_phone, $mike_phone, $james_phone);              
        $filtered_phone = array_filter($phone_array);           
        $cc = implode(', ', $filtered_phone);           
        if (!empty($mail_to))
            sendEmail($mail_to, $drive_url, $date_of_show, $cc);                
    }
}

数据库结果:

ID  EID     drive_folder    drive_folder_name   drive_folder_empty  duncan  robert  mike    james   partners_name   completed_form  all_forms_in    priority    ask_for_review  blog_status     bloggers_email  todays_date     show_date
20  2457    drive url       drive name          empty               Duncan  Robert  NULL    NULL    NULL            n               n               NULL        NULL            NULL            NULL            2017-04-24      2017-04-29 
21  2468    drive url       drive name          empty               NULL    NULL    Mike    James   NULL            n               n               NULL        NULL            NULL            NULL            2017-04-24      2017-04-29

发生了什么:

Array
(
  [0] => robert email
  [1] => duncan email
)
Array
(
  [0] => robert email
  [1] => duncan email
  [2] => mike email
  [3] => james email
)

我想要发生什么

Array
(
  [0] => robert email
  [1] => duncan email
)
Array
(
  [0] => mike email
  [1] => james email
)

为什么它会在第二次运行时保留以前的值?

2 个答案:

答案 0 :(得分:1)

试试这个,

  $query = mysql_query("SELECT * FROM tasks WHERE DATE(`show_date`) = (      CURDATE() - INTERVAL 1 DAY ) AND drive_folder_empty = 'empty' AND drive_folder IS NOT NULL;");
  while ($row = mysql_fetch_assoc($query)){
     $robert_email = $duncan_email = $mike_email = $james_email = $robert_phone = $duncan_phone = $mike_phone = $james_phone = '';
    if (!empty($row['robert'])){$robert_email = "robert email"; $robert_phone = "robert phone";}
    if (!empty($row['duncan'])){$duncan_email = "duncan email"; $duncan_phone = "duncan phone";}
    if (!empty($row['mike'])){$mike_email = "mike email"; $mike_phone = "mike phone";}
    if (!empty($row['james'])){$james_email = "james email"; $james_phone = "james phone";}


    $email_array = array($robert_email, $duncan_email, $mike_email, $james_email);              
    $filtered_email = array_filter($email_array);

    print_r($filtered_email);

    $mail_to = implode(', ', $filtered_email);

    $phone_array = array($robert_phone, $duncan_phone, $mike_phone, $james_phone);              
    $filtered_phone = array_filter($phone_array);           
    $cc = implode(', ', $filtered_phone);           
    if (!empty($mail_to))
        sendEmail($mail_to, $drive_url, $date_of_show, $cc);                
   }
}

在这种情况下回答你的问题。在使用之前,您需要在循环中重置变量。

答案 1 :(得分:0)

您的问题是,您要为每个人设置电子邮件'变量,但你不是每次都将它们设置为空白。

但是,您不应该有条件地设置这些变量,您应该有条件地加载email_arrayphone_array数组,具体取决于他们的姓名'列填充在数据库中。

这样的事情会更清洁一些:

$people = Array(
    'robert' => Array(
        'email' => 'robert email',
        'phone' => 'robert phone'
    ),
    'duncan' => Array(
        'email' => 'duncan email',
        'phone' => 'duncan phone'
    ),
    'mike' => Array(
        'email' => 'mike email',
        'phone' => 'mike phone'
    ),
    'james' => Array(
        'email' => 'james email',
        'phone' => 'james phone'
    )
);



while ($row = mysql_fetch_assoc($query)) {

    // loop through each person and load up
    // the arrays if the name col is not empty
    foreach ($people as $name => $contact_info) {
        if ($row[$name]) {
            $email_array[] = $contact_info['email'];
            $phone_array[] = $contact_info['phone'];
        }
    }

    if (count($email_array) > 0) {
        $mail_to = implode(', ', $email_array);
        $cc = implode(', ', $phone_array);

        sendEmail($mail_to, $drive_url, $date_of_show, $cc);

        $email_array = Array();
        $phone_array = Array();
    }
}

请注意,您不需要过滤数组,因为它们始终包含正确的信息。