Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 => '[email protected]',
1 => '[email protected]',`.....`
4000 => '[email protected]');

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 <[email protected]>' . "\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.

share|improve this question
    
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 Answer 1

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

           $headers= 'From: Ldamsin <[email protected]>' . "\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.

share|improve this answer
    
Now I changed my code , i assigned to a temporary array, then its working fine. –  Pramod Jun 11 '13 at 12:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.