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

时间:2017-02-14 10:33:23

标签: php arrays

我的PHP代码看起来像这样

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>

上面的代码显示JSON输出,如下所示

{"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"}

我的问题是如何修改上面的PHP代码以使JSON输出显示如下?我的意思是单次登录有多个“消息”值

{
    "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"
                    },
                ]
}

感谢有人可以提供帮助。感谢。

1 个答案:

答案 0 :(得分:1)

创建两个数组,在第一个数组中给出login,realname,在第二个数组中给出refno,email,dept,division然后将第二个数组合并到第一个数组,然后使用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);


  ?>