Make this algorithm more efficent
<p>I'm a php noob, and need some help. I've build a push service to send out push messages to an iphone. It says in the comments that it's more efficient to combine several messages into one packet. Also, right now I make a call to delete each and every message. How do I combine several messages into one package? </p>

<p>Thanks  </p>

<pre><code>function start()
{
    writeToLog('Connecting to ' . $this-&gt;server);

    if (!$this-&gt;connectToAPNS())
        exit;


    while (true)
    {
        // Do at most 20 messages at a time. Note: we send each message in
        // a separate packet to APNS. It would be more efficient if we 
        // combined several messages into one packet, but this script isn't
        // smart enough to do that. ;-)

        $stmt = $this-&gt;pdo-&gt;prepare('SELECT * FROM push_queue WHERE time_sent IS NULL LIMIT 20');
        $stmt-&gt;execute();
        $messages = $stmt-&gt;fetchAll(PDO::FETCH_OBJ);

        foreach ($messages as $message)
        {
            if ($this-&gt;sendNotification($message-&gt;message_id, $message-&gt;device_token, $message-&gt;payload))
            {
                //$stmt = $this-&gt;pdo-&gt;prepare('UPDATE push_queue SET time_sent = NOW() WHERE message_id = ?');
                //$stmt-&gt;execute(array($message-&gt;message_id));

                $stmt = $this-&gt;pdo-&gt;prepare('DELETE FROM push_queue WHERE message_id = ?');
                $stmt-&gt;execute(array($message-&gt;message_id));

            }
            else  // failed to deliver
            {
                $this-&gt;reconnectToAPNS();
            }
        }

        unset($messages);           
        sleep(5);
    }
}
</code></pre>
