0

I need your advice and help

i fetched an array of data from the database and i want to process each element one by one without using foreach loop, something like

pop element a and process it, when finished pop element b and process it, when finished pop element b and process it

until the array become empty then the script can exit

currently i`m looping through the data using foreach loop but things are not working find.

$loaded_message = $this->lib->load_queued_messages();

                if(count($loaded_message) == 0) {
                    die ('Nothing to do');
                }

               foreach($loaded_message as $tosend)
               {


                if($this->lib->send_sms($tosend['from'], $tosend['msg'], explode(',', $tosend['numbers']), $tosend['owner'], $tosend['qid']))
                {

                    // Remove the message from queue
                    $this->lib->remove_msg_from_queued_message($tosend['qid']);
                    $this->lib->log('message #' . $tosend['qid']. ' sent and removed from queue', $tosend['owner']);
                }else{
                    $this->lib->log('SENDING_ERROR: message #' . $tosend['qid']. ' not sent and remain in the queue for#', $tosend['owner']);
                }
               }

Inside the log table i discovered that entry was made for wrong message id and it seems like message was sent to wrong number but it does not.

hakre
  • 193,403
  • 52
  • 435
  • 836
elf1984
  • 301
  • 1
  • 4
  • 12
  • post what you have that is not working, so that someone can tell you how to fix it. – Mat Apr 16 '11 at 10:00
  • 4
    Pardon the question, why are you avoiding the loop? – Damien Pirsy Apr 16 '11 at 10:06
  • Post updated with sample code. thanks to @Damien and @Mat for your fast reply – elf1984 Apr 16 '11 at 10:17
  • Even with pop element you need a loop, a while loop. Pseudocode: while(!array_empty()) $tmpElem = pop_element; – strauberry Apr 16 '11 at 10:27
  • You could consider using an SPL stack - http://uk.php.net/manual/en/class.splstack.php - but you're still going to have to loop until it's empty – Mark Baker Apr 16 '11 at 10:31
  • thanks to everyone, i fixed the bug, the problem was from another log class, all i need was to reset an array so previous elements are not re-logged and that`s all – elf1984 Apr 16 '11 at 23:10

1 Answers1

0

hi mate you can use something like

while(sizeof($yourarray)) {
  $result = array_pop(yourarray);
  ...yourprocessing_here(...);
}

hope this helps :)

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36