如果最后一个来自同一用户,则对邮件进行分组

I have a chat and the messages are printed more or less like this:

Jhon: Hey!

Jhon: Fine?

Smith: Yeah, I'm fine. Thanks.

Jhon: That's nice!

To print that i have the code:

array(4) {
  [0]=>
  array(3) {
    ["from"]=>
    string(4) "Jhon"
    ["to"]=>
    string(5) "Smith"
    ["msg"]=>
    string(4) "Hey!"
  }
  [1]=>
  array(3) {
    ["from"]=>
    string(4) "Jhon"
    ["to"]=>
    string(5) "Smith"
    ["msg"]=>
    string(5) "Fine?"
  }
  [2]=>
  array(3) {
    ["from"]=>
    string(5) "Smith"
    ["to"]=>
    string(4) "Jhon"
    ["msg"]=>
    string(23) "Yeah, I'm fine. Thanks."
  }
  [3]=>
  array(3) {
    ["from"]=>
    string(4) "Jhon"
    ["to"]=>
    string(5) "Smith"
    ["msg"]=>
    string(12) "That's nice!"
  }
}

I want to show the messages like this:

Jhon: Hey!

Fine?

Smith: Yeah, I'm fine. Thanks.

Jhon: That's nice!

Thank you and sorry for my English.

A little example to get you started:

<?php

$messages = array(
    array(
        "From" => "A",
        "To" => "B",
        "Msg" => "A first message"
    ),
    array(
        "From" => "A",
        "To" => "B",
        "Msg" => "A second message"
    ),
    array(
        "From" => "B",
        "To" => "A",
        "Msg" => "B first message"
    )
);

$previousSender = null;
foreach($messages as $message) {
    if($previousSender == null || $previousSender != $message["From"])
        printf("%s: %s", $message["From"], $message["Msg"]);
    else
        printf("%s", $message["Msg"]);  

    $previousSender = $message["From"];
}

?>