Q:

Make this algorithm more efficent

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? 
Thanks  
function start()
{
    writeToLog('Connecting to ' . $this->server);

    if (!$this->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->pdo->prepare('SELECT * FROM push_queue WHERE time_sent IS NULL LIMIT 20');
        $stmt->execute();
        $messages = $stmt->fetchAll(PDO::FETCH_OBJ);

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

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

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

        unset($messages);           
        sleep(5);
    }
}

A:

You can group deleted messages IDs inside array
$deletedIds = array()
foreach ($messages as $message)
{
    if ($this->sendNotification())
    {
        $deletedIds[] = $message->message_id;
    }
}

After that you can delete such messages using query method 
$this->pdo->query('DELETE FROM push_queue WHERE message_id IN ('.implode(',', $deletedIds).')');

