该PHP应该返回4行数据。
   A行,B行,C行,D行

$query = $mysqli->query("select codeid,description from codeTable where tableCode='TABLE'");
$json = array();
if($query->num_rows){
    while($tblArray[] = $query->fetch_object()){
    $json[]=$tblArray;
    }
}
echo json_encode($json);


但实际上,json_encode显示的是10行数据。
   A行
   A行,B行,
   A行,B行,C行,
   A行,B行,C行,D行

[[{"codeid":"4","description":"Document Type"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"},{"codeid":"1","description":"Note Type"}],[{"codeid":"4","description":"Document Type"},{"codeid":"8","description":"Images"},{"codeid":"1","description":"Note Type"},{"codeid":"5","description":"Projects"}]]


有人可以告诉我为什么会这样吗?最终目标是将查询返回的四行数据放入一个javascript数组中。

最佳答案

// remove [] here, or you are adding the row data to an array, then add to another array.
while($tblArray = $query->fetch_object()){
   $json[] = $tblArray;
}

10-06 05:49