我怎样才能给我的json数组命名

时间:2018-01-19 18:13:35

标签: php arrays json

所以我创建了以下php文件。在这个php脚本中,它连接到我的在线数据库,从表客户端获取所有行,并存储在JSON数组中。但是当我运行php文件时,它会显示如下数据:

SELECT [from],
       [to],
       CASE WHEN [from] < [to] THEN [from] + [to]
            ELSE [to] + [from]
       END AS seg
FROM   mytable

这个数组没有数组名,我怎么能用这个PHP脚本给这个数组命名。

jsonclient.php

[{
    "0": "1",
    "client_id": "1",
    "1": "Tango",
    "client_name": "Tango",
    "2": "1234567890",
    "client_mobile": "1234567890",
    "3": "tango@gmail.com",
    "client_email": "tango@gmail.com"
}, {
    "0": "2",
    "client_id": "2",
    "1": "yoyo",
    "client_name": "yoyo",
    "2": "123",
    "client_mobile": "123",
    "3": "yoyo",
    "client_email": "yoyo"
}]

3 个答案:

答案 0 :(得分:0)

$result = ["ANY_NAME" => $flag];
$jsonResult = json_encode($result);

答案 1 :(得分:0)

$flag = array( 'nameGoesHere' => array());
while($row=mysqli_fetch_array($query))
{
     $flag['nameGoesHere'][] = $row;
}

答案 2 :(得分:0)

<?php
    require "init.php";

    $sql_jason = "SELECT * FROM clients";

    if(mysqli_connect_error($con)){
        echo "cannot connect";
    }

    $query = mysqli_query($con,"SELECT * FROM clients");
    if($query)
    {
        while($row=mysqli_fetch_array($query))
        {
            $flag[] = $row;
        }

        $flags = ["flags" => $flag];
        print(json_encode($flags));
    }
    mysqli_close($con);
?>

这应该有效