dongluanan7163 2012-03-23 11:12
浏览 46
已采纳

删除函数PHP中的重复数组值

I read all topics related to this question in stackoverflow and whole internet and cant find working sollution... Each owner has his item and when someone buy his item, owner gets an confirmation email, but when in cart is few same owner items, he gets several same email letters, so I need to remove dublicated array entries. I have tried to use DISTINCT and array_uniques functions but no luck. Any advices?

I have an array and function to send mail..

function email($mail_array) {
  foreach(array_unique($mail_array) as $field => $value) {
    $result = mysql_query("select email from users where $field='$value'");
    $row = mysql_fetch_array($result);


       $maail = mysql_real_escape_string($row['email']);
}
        $email_to = "".$maail."";



    // rest of mail formatting code

    // create email headers
    $headers = 'From: '.$email_from."
" .
               'Reply-To: '.$email_from."
" .
               'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
}


for ($i = 0; $i < $max; $i++) {
  $pid = $_SESSION['cart'][$i]['productid'];
  $owner = get_owner($pid);
  $mail_array = array(
    'name' => $owner
  );
  email($mail_array)  //call function to send mail          
}
  • 写回答

4条回答 默认 最新

  • dragon8837 2012-03-23 11:26
    关注

    You are passing $mail_array with a single value to mail function,so multiple mails are being sent. Instead, make $owner as array() and push all owners name in it.But before that check whether the array has the same owner already using in_array(). If owner is not there in the array then push it in the array otherwise do next iteration. And pass this array to mailing function.

        $mail_array = array();
        for ($i = 0; $i < $max; $i++) {
        $pid = $_SESSION['cart'][$i]['productid'];
        $owner = get_owner($pid);
        $mail_array = array_push($mail_array,$owner);    
        }
        email($mail_array)  //call function to send mail   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计