php查询到json

7dr*_*am7 2 javascript php arrays json associative

我正在查询我的数据库Select * from Customer,客户表中包含姓名,姓氏地址,年龄.

我希望能够将查询转换为以下对象中的json对象:

顾客:

[
    {Name:"john", Surname:"Beta" ,Age:"23"},
    {Name:"Fred", Surname:"alpha" ,Age:"31"}
];
Run Code Online (Sandbox Code Playgroud)

你有任何想法吗?我试图循环查询并使用merge_array ..但它按预期合并了阵列...谢谢你的时间.

mar*_*rio 6

您只需要分组到预期的嵌套结构:

while ($row = mysql_fetch_assoc($r)) {
    $customer[] = $row;
}

$struct = array("Customer" => $customer);
print json_encode($struct);
Run Code Online (Sandbox Code Playgroud)