如何在PHP mysqli中编码JSON输出?

my PHP code look like this

test.php

<?php
    $connect = mysqli_connect("","","","");
    global $connect;

    if (isset($_POST['login']) )
    {
        $login = $_POST['login'];

        $sql   = "SELECT * FROM table WHERE login='$login'";
        $result = mysqli_query($connect, $sql);
        if ($result && mysqli_num_rows($result) > 0){
            while ($row = mysqli_fetch_array($result)) {

                $login_db    = $row['login'];
                $real_namedb = $row['real_name'];
                $email_db    = $row['email'];
                $dept_db     = $row['dept'];
                $division_db = $row['division'];

                $output= array('messages' => '1', 
                                                'login' => $login_db,
                                                'real_name' => $real_namedb,     
                                                'email' => $email_db, 
                                                'dept' => $dept_db, 
                                                'division' => $division_db
                               );
                echo json_encode($output);
                exit();
            }
        mysqli_free_result($result);
        }
        else {
            $output = array('messages' => '2', 'login' => 'wrong credentials from PHP code!');
            echo json_encode($output);
            echo mysqli_error($connect);
            exit();
        }
    }
    else
    {
        $output = array('messages' => '3', 'login' => 'No post data');
        echo json_encode($output);
        exit();
    }
?>
<!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
    <form action="test.php" method="post">  
        <table>
            <tr>
                <td><i class="fa fa-unlock-alt"></i> </td>
                <td>Email : </td>
                <td><input type ="text" name="login" size="30"></td>
            </tr>
        </table>    

        <p><input type ="submit" name="Submit" value="DISPLAY"> </p>             
    </form>
</body>
</html>

My code above display JSON output like this

{"messages":"1","login":"ID0111","real_name":"NAME HERE","email":"mail@mail.com","dept":"IT","division":"MDO"}
{"messages":"1","login":"ID0112","real_name":"NAME HERE2","email2":"mail@mail.com","dept":"IT","division":"MDO"}

My question is how to modify PHP code above to make JSON output display like this ? What I mean is single login has multiple "messages" value

{
    "login":"ID0111",
    "real_name":"NAME HERE",
    "messages":
                [
                    {
                        "refno":"1234",
                        "email":"mail@mail.com",
                        "dept":"IT",
                        "division":"MDO"
                    },
                    {
                        "refno":"1345",
                        "email":"mail2@mail.com",
                        "dept":"IT",
                        "division":"MDO"
                    },
                ]
}

Appreciate if someone can help. Thanks.

Create to two arrays,in first array give login,realname and in second array give refno,email,dept,division then merge the second with first array and then use json_encode().

  <?php

    $result =array();
    $message=array();

    $result['login'] ='ID0111';
    $result['real_name'] ='NAME HERE';
    $message['refno'] ='1234';
    $message['email'] ='mail@mail.com';
    $message['dept'] ='IT';
    $message['division'] ='MDO';
    $result['message'] =$message;

    echo json_encode($result);


  ?>