<?php
$link = mysqli_connect("localhost", "root", "", "college");
$query = "SELECT branch, year,semester, COUNT(*) count FROM students GROUP BY branch,year,semester;";
$result = mysqli_query($link,$query);
$rows= mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode($rows);
?>
这是我的输出:
[{“ branch”:“ cse”,“ year”:“ 1”,“ semester”:“ 1”,“ count”:“ 4”},{“ branch”:“ cse”,“ year” :“ 2”,“学期”:“ 1”,“计数”:“ 1”},{“分支”:“ cse”,“年份”:“ 2”,“学期”:“ 2”,“计数” :“ 2”},{“ branch”:“ cse”,“ year”:“ 2”,“学期”:“ 4”,“ count”:“ 1”},{“ branch”:“ cse”,“ year“:” 3“,” semester“:” 2“,” count“:” 1“},{” branch“:” cse“,” year“:” 3“,” semester“:” 3“,” count“:” 2“},{” branch“:” cse“,” year“:” 4“,” semester“:” 3“,” count“:” 1“},{” branch“:” cse“ ,“ year”:“ 4”,“学期”:“ 4”,“ count”:“ 2”}, {“ branch”:“ ece”,“ year”:“ 1”,“ semester”:“ 4”,“ count”:“ 2”},{“ branch”:“ ece”,“ year”:“ 2” ,“ semester”:“ 2”,“ count”:“ 2”},{“ branch”:“ ece”,“ year”:“ 2”,“ semester”:“ 3”,“ count”:“ 1” },{“ branch”:“ ece”,“ year”:“ 3”,“ semester”:“ 1”,“ count”:“ 1”},{“ branch”:“ ece”,“ year”:“ 3“,” semester“:” 3“,” count“:” 2“},{” branch“:” ece“,” year“:” 4“,” semester“:” 2“,” count“:” 2“},{” branch“:” ece“,” year“:” 4“,” semester“:” 4“,” count“:” 2“}, {“ branch”:“ eee”,“ year”:“ 1”,“ semester”:“ 1”,“ count”:“ 1”},{“ branch”:“ eee”,“ year”:“ 1” ,“ semester”:“ 4”,“ count”:“ 1”},{“ branch”:“ eee”,“ year”:“ 2”,“ semester”:“ 1”,“ count”:“ 1” },{“ branch”:“ eee”,“ year”:“ 2”,“ semester”:“ 2”,“ count”:“ 3”},{“ branch”:“ eee”,“ year”:“ 2“,”学期“:” 3“,”计数“:” 2“},{”分支“:” eee“,”年份“:” 2“,”学期“:” 4“,”计数“:” 1“},{” branch“:” eee“,” year“:” 3“,” semester“:” 1“,” count“:” 1“},{” branch“:” eee“,” year“ :“ 3”,“学期”:“ 2”,“计数”:“ 1”},{“分支”:“ eee”,“年份”:“ 3”,“学期”:“ 3”,“计数” :“ 4”},{“ branch”:“ eee”,“ year”:“ 3”,“ semester”:“ 4”,“ count”:“ 3”},{“ branch”:“ eee”,“ year“:” 4“,” semester“:” 2“,” count“:” 3“},{” branch“:” eee“,” year“:” 4“,” semester“:” 3“,” count“:” 2“},{” branch“:” eee“,” year“:” 4“,” semester“:” 4“,” count“:” 1“}]
但是我想要这样的输出:
{"cse":
[{"year":"1","semester":"1","count":"4"},
{"year":"2","semester":"1","count":"1"},
{"year":"2","semester":"2","count":"2"},
{"year":"2","semester":"4","count":"1"},
{"year":"3","semester":"2","count":"1"},
{"year":"3","semester":"3","count":"2"},
{"year":"4","semester":"3","count":"1"},
{"year":"4","semester":"4","count":"2"}],
"ece":
[{"year":"1","semester":"4","count":"2"},
{"year":"2","semester":"2","count":"2"},
{"year":"2","semester":"3","count":"1"},
{"year":"3","semester":"1","count":"1"},
{"year":"3","semester":"3","count":"2"},
{"year":"4","semester":"2","count":"2"},
{"year":"4","semester":"4","count":"2"}],
"eee":
[{"year":"1","semester":"1","count":"1"},
{"year":"1","semester":"4","count":"1"},
{"year":"2","semester":"1","count":"1"},
{"year":"2","semester":"2","count":"3"},
{"year":"2","semester":"3","count":"2"},
{"year":"2","semester":"4","count":"1"},
{"year":"3","semester":"1","count":"1"},
{"year":"3","semester":"3","count":"4"},
{"year":"3","semester":"4","count":"3"},
{"year":"4","semester":"2","count":"3"},
{"year":"4","semester":"3","count":"2"},
{"year":"4","semester":"4","count":"1"}]}
建议以其他任何方式在输出上方显示
答案 0 :(得分:1)
比起mysqli_fetch_all
而言,一次提取每一行并通过一次操作将其放入正确的格式可能更容易(并且可能更快)...
$result = mysqli_query($link,$query);
$output = [];
while ( $rows= mysqli_fetch_assoc($result)) {
$output [$rows['branch']][] = ["year" => $rows['year'],
"semester" => $rows['semester'],
"count" => $rows['count']];
}
echo json_encode($output);