PHP - 如何从 MySQL 创建多维 JSON?
PHP - How to create multidimentional JSON from MySQL?
我需要从两个 MySQL table 创建一个多维 JSON。我的代码有效,但我认为它写得非常糟糕,因为它首先连接数据库值,然后在 PHP 中展开结果字符串。必须有更好(更快)的方法来做到这一点。
我知道类似的问题已被问过很多次,但我是菜鸟,不知道如何将答案应用到我的案例中。
所以,我有以下 MySQL tables:
tbl1
-----------------------------
id| question |correctanswer
-----------------------------
1|'Yes or no?'| 'Yes'
2| 'Who?' | 'Peter'
tbl2
-----------------------------
id|questionid| answeroptions
-----------------------------
1 | 1|'Yes'
2 | 1|'No'
3 | 2|'Peter'
4 | 2|'John'
5 | 2|'James'
6 | 2|'Jack'
我想像这样构建 JSON 个字符串:
{
"id":"1",
"question":"Yes or no?",
"correct_answer":"Yes",
"answeroptions":[
"Yes",
"No"
]
}
{
"id":"2",
"question":"Who?",
"correct_answer":"Peter",
"answeroptions":[
"Peter",
"John",
"James",
"Jack"
]
}
这是我的代码:
$sql = "SELECT tabl1.id, tabl1.question, tabl1.correctanswer,
GROUP_CONCAT(DISTINCT tbl2.answeroptions)
FROM tabl1
LEFT JOIN tbl2 ON (tabl1.id = tbl2.questionid)
GROUP BY tabl1.id;";
$jsonarray = array();
$result = $conn->query($sql);
$i = 0;
while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
$jsonarray[$i]["topic"] = $row[0];
$jsonarray[$i]["id"]=$row[1];
$jsonarray[$i]["question"]=$row[2];
$jsonarray[$i]["correct_answer"]=$row[3];
$jsonarray[$i]["answeroptions"] = array();
$jsonarray[$i]["answeroptions"] = explode(",",$row[4]);
$i++;
}
for ($d=0; $d<count($jsonarray);$d++){
echo json_encode($jsonarray[$d])."<br>";
}
更新:
我对三个用例进行了性能测试:
- 布莱克的建议
- ded 的建议
- 将 answeroptions 合并到 tbl1 中的新列中以消除第二个请求并使用相应修改的 ded 脚本。
我使用了 for 循环 * 100 次,结果如下:
- 0.16780591011047 秒
- 0.0067291259765625 秒
- 0.0005500316619873 秒
因此,根据性能,我得出一个结论,即在数据库中使用一对多关系并不总是最佳选择。对于这样简单的任务,所有数据都可以存储在同一个 table.
谢谢大家的帮助!
您真的应该执行两次查询以获得最佳性能。试试这个代码(未经测试)
# Question array holder
$questions = [];
# Loading the questions
$query = " SELECT
`id`,
`question`,
`correctanswer`
FROM
`tabl1`";
$result = mysqli_query( $conn, $query );
# Nothing to send
if( mysqli_num_rows( $result ) == 0 ) {
exit( '[]' );
}
# Adding questions to array
while( $question_data = mysqli_fetch_assoc( $result ) ) {
$question_data[ 'answeroptions' ] = [];
$questions[ $question_data[ 'id' ] ] = $question_data;
}
# Loading the answer
$query = " SELECT
*
FROM
`tabl2`
WHERE
`questionid` IN (" . implode( ',', array_keys( $questions ) ) . ")";
$result = mysqli_query( $conn, $query );
# Adding answers to array
while( $answer_id = mysqli_fetch_assoc( $result ) ) {
$questions[ $answer_id[ 'questionid' ] ][ 'answeroptions' ][] = $answer_id[ 'answeroptions' ];
}
# Sending the questions
echo json_encode( array_values( $questions ) );
$sql = "SELECT tabl1.id, tabl1.question, tabl1.correctanswer,
GROUP_CONCAT(DISTINCT tbl2.answeroptions)
FROM tabl1
LEFT JOIN tbl2 ON (tabl1.id = tbl2.questionid)
GROUP BY tabl1.id;";
$result = $conn->query($sql);
$jsonarray = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($jsonarray as $row) {
$row["answeroptions"] = explode(",", $row["answeroptions"]);
}
echo json_encode($jsonarray)."<br>";
我需要从两个 MySQL table 创建一个多维 JSON。我的代码有效,但我认为它写得非常糟糕,因为它首先连接数据库值,然后在 PHP 中展开结果字符串。必须有更好(更快)的方法来做到这一点。
我知道类似的问题已被问过很多次,但我是菜鸟,不知道如何将答案应用到我的案例中。
所以,我有以下 MySQL tables:
tbl1
-----------------------------
id| question |correctanswer
-----------------------------
1|'Yes or no?'| 'Yes'
2| 'Who?' | 'Peter'
tbl2
-----------------------------
id|questionid| answeroptions
-----------------------------
1 | 1|'Yes'
2 | 1|'No'
3 | 2|'Peter'
4 | 2|'John'
5 | 2|'James'
6 | 2|'Jack'
我想像这样构建 JSON 个字符串:
{
"id":"1",
"question":"Yes or no?",
"correct_answer":"Yes",
"answeroptions":[
"Yes",
"No"
]
}
{
"id":"2",
"question":"Who?",
"correct_answer":"Peter",
"answeroptions":[
"Peter",
"John",
"James",
"Jack"
]
}
这是我的代码:
$sql = "SELECT tabl1.id, tabl1.question, tabl1.correctanswer,
GROUP_CONCAT(DISTINCT tbl2.answeroptions)
FROM tabl1
LEFT JOIN tbl2 ON (tabl1.id = tbl2.questionid)
GROUP BY tabl1.id;";
$jsonarray = array();
$result = $conn->query($sql);
$i = 0;
while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
$jsonarray[$i]["topic"] = $row[0];
$jsonarray[$i]["id"]=$row[1];
$jsonarray[$i]["question"]=$row[2];
$jsonarray[$i]["correct_answer"]=$row[3];
$jsonarray[$i]["answeroptions"] = array();
$jsonarray[$i]["answeroptions"] = explode(",",$row[4]);
$i++;
}
for ($d=0; $d<count($jsonarray);$d++){
echo json_encode($jsonarray[$d])."<br>";
}
更新: 我对三个用例进行了性能测试:
- 布莱克的建议
- ded 的建议
- 将 answeroptions 合并到 tbl1 中的新列中以消除第二个请求并使用相应修改的 ded 脚本。
我使用了 for 循环 * 100 次,结果如下:
- 0.16780591011047 秒
- 0.0067291259765625 秒
- 0.0005500316619873 秒
因此,根据性能,我得出一个结论,即在数据库中使用一对多关系并不总是最佳选择。对于这样简单的任务,所有数据都可以存储在同一个 table.
谢谢大家的帮助!
您真的应该执行两次查询以获得最佳性能。试试这个代码(未经测试)
# Question array holder
$questions = [];
# Loading the questions
$query = " SELECT
`id`,
`question`,
`correctanswer`
FROM
`tabl1`";
$result = mysqli_query( $conn, $query );
# Nothing to send
if( mysqli_num_rows( $result ) == 0 ) {
exit( '[]' );
}
# Adding questions to array
while( $question_data = mysqli_fetch_assoc( $result ) ) {
$question_data[ 'answeroptions' ] = [];
$questions[ $question_data[ 'id' ] ] = $question_data;
}
# Loading the answer
$query = " SELECT
*
FROM
`tabl2`
WHERE
`questionid` IN (" . implode( ',', array_keys( $questions ) ) . ")";
$result = mysqli_query( $conn, $query );
# Adding answers to array
while( $answer_id = mysqli_fetch_assoc( $result ) ) {
$questions[ $answer_id[ 'questionid' ] ][ 'answeroptions' ][] = $answer_id[ 'answeroptions' ];
}
# Sending the questions
echo json_encode( array_values( $questions ) );
$sql = "SELECT tabl1.id, tabl1.question, tabl1.correctanswer,
GROUP_CONCAT(DISTINCT tbl2.answeroptions)
FROM tabl1
LEFT JOIN tbl2 ON (tabl1.id = tbl2.questionid)
GROUP BY tabl1.id;";
$result = $conn->query($sql);
$jsonarray = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach ($jsonarray as $row) {
$row["answeroptions"] = explode(",", $row["answeroptions"]);
}
echo json_encode($jsonarray)."<br>";