-1

Hello all i have major problem, i have an array with length greater than 4000. In a foreach loop i tried to unset each key but its not working properly.

$arr=array(  0 => '365-pramo@mageos.com',
1 => '365-pram@mageos.com',`.....`
4000 => '5333-pram123@mageos.com');

Here i need to get each mail id and user id from this array(its actually getting from a table) i will send mail to each mail id and update that in my table. Here is my code.

if(!empty($uids_eidsArr)){
           foreach($uids_eidsArr as $k=>$v){
              //echo $v;
              // echo $r->id;('-',
               $eArr=  explode('-', $v);

                   $headers= 'From: Ldamsin <admin@domain.org>' . "\r\n";
                   $headers.="MIME-Version: 1.0\r\n";
                   $headers.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";

                   $content = $res_dup[0]->newsletter_content;

                  @mail($eArr[1], $res_dup[0]->newsletter_subject, $content, $headers); // mail to client
                    $this->db->set('user_id',$sent_by);
                    $this->db->set('sent_to',$eArr[0]);
                    $this->db->set('isSubscriber',$isSubscriber);
                    $this->db->set('content_id',$newsId);   
                    $this->db->insert('newsletter_senthistory');
                  // echo '<pre>';print_r($uids_eidsArr);echo '</pre>';
                    unset($uids_eidsArr[$k]);
                    //echo '<pre>';print_r($uids_eidsArr);echo '</pre>';
                   sleep(36);


           }  }

But it is not unset all keys . I don't know what's the problem. Is it because of sleep function. Because of this some users getting more than 40 mails each time. Please help me.

Pramod
  • 1,031
  • 3
  • 13
  • 26
  • How exactly are you checking that the key has not been unset? Are you saying that the `print_r($uids_eidsArr)` after the `unset` shows the key as still being there? I really doubt that. – deceze Jun 11 '13 at 12:24
  • yes, this is not for all key and not for a small array.. – Pramod Jun 11 '13 at 12:26
  • You can check if the entire code is being called in some outer loop or by some way may be called multiple times, I would do some logic to check that, insert dummy entry into DB or write into file to check that. – Minesh Jun 11 '13 at 12:28
  • maybe table update section has problem. did you check db? – MC_delta_T Jun 11 '13 at 12:30
  • In db multiple entries are coming. It is working properly for small array, that is i wondering. – Pramod Jun 11 '13 at 12:31
  • 1
    I don't see what good the `unset` does you in the first place. You're unsetting the key you just iterated over, but you're never going back to it. So this is not preventing any sort of duplicates within this iteration anyway. – deceze Jun 11 '13 at 12:40

1 Answers1

1
$sentUser=array();
if(!empty($uids_eidsArr)){
    foreach($uids_eidsArr as $k=>$v){
      //echo $v;
      // echo $r->id;('-',
       $eArr=  explode('-', $v);

           $headers= 'From: Ldamsin <admin@domain.org>' . "\r\n";
           $headers.="MIME-Version: 1.0\r\n";
           $headers.="Content-Type: text/html;\n\tcharset=\"iso-8859-1\"\r\n";

           $content = $res_dup[0]->newsletter_content;
            if(!in_array($eArr[0], $sentUser)){
                @mail($eArr[1], $res_dup[0]->newsletter_subject, $content, $headers); // mail to client
                $this->db->set('user_id',$sent_by);
                $this->db->set('sent_to',$eArr[0]);
                $this->db->set('isSubscriber',$isSubscriber);
                $this->db->set('content_id',$newsId);   
                $this->db->insert('newsletter_senthistory');
              // echo '<pre>';print_r($uids_eidsArr);echo '</pre>';
                unset($uids_eidsArr[$k]);//it is not required
                //echo '<pre>';print_r($uids_eidsArr);echo '</pre>';
               sleep(36);
               array_push($sentUser, $eArr[0]);
            }



    }  

I think same user is being fetched multiple times in your array. You must refine your query. I have given above an alternate solution.

Amit Garg
  • 3,867
  • 1
  • 27
  • 37