我正在尝试将MySQL表格列输出为JSON格式。在我的查询之后,在PHP中我这样做:
$json_data=array();
while ($m = mysqli_fetch_array($query))
{
$json_data[] = array('name' => 'Model', 'data' => $m[1]);
}
print json_encode($json_data, JSON_NUMERIC_CHECK);
$m[1]
只是表中包含数值的列。我的尝试产生了这样的结果:
[{"name":"Model","data":0},{"name":"Model","data":58},{"name":"Model","data":0},
{"name":"Model","data":0},{"name":"Model","data":84},{"name":"Model","data":15},
{"name":"Model","data":0},{"name":"Model","data":12},{"name":"Model","data":0},
{"name":"Model","data":102},{"name":"Model","data":0}]
但是,我需要格式看起来像这样(请注意,我不希望在开头/结尾有周围的[
和]
):
{"name":"Model","data":[0, 58, 0, 0, 84, 15, 0, 12, 0, 102, 0]}
有谁知道如何实现这个目标?
答案 0 :(得分:2)
使用类似的东西
$json_data=array();
$json_data['name']='Model';
while ($m = mysqli_fetch_array($query))
{
$json_data['data'][] = $m[1];
}
print json_encode($json_data, JSON_NUMERIC_CHECK);
答案 1 :(得分:0)
这应该有效
while ($m = mysqli_fetch_array($query))
{
$json_data[] = $m[1];
}
print json_encode(['name'=>'Model', 'data'=>$json_data], JSON_NUMERIC_CHECK);