gpt4 book ai didi

php - 在 PHP 中将二维数组转换为 JSON

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:21:30 25 4
gpt4 key购买 nike

我的目标是使单个数据库命中以生成一个对象的 JSON 数组,该数组具有的属性也可以是对象数组,也可以具有任意(但已知)深度的对象数组等属性。

我使用 PDO 对象从数据库中获取二维数组。

$stmt = $this->pdo->prepare($sql);
$stmt->execute($args);
$TwoDimArray = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $TwoDimArray;

所以在我的例子中,我有一个测验,它可以有问题,也可以有答案。所以深度为 3。假设我有一个二维数组,如下表所示,我将如何使用该数据制作这个 JSON 数组。 (注意:为清楚起见,已删除其他数据,但问题和答案可能具有其他属性。)

An example of a 2D array to the desired JSON object

编辑:这是我的尝试。对于一般情况,它效率低得可怕,而且不是很抽象,但这段代码将为小数据集提供正确的答案。

$data = ($database -> query($sql,$args)); // the result is $TwoDimArray
$quizes = array();
$quiz_id_list = array();
foreach($data as $quizkey => $quizvalue){
if(!in_array($quizvalue["quiz_id"], $quiz_id_list)) {
array_push($quiz_id_list, $quizvalue["quiz_id"]);
$question_id_list = array();
$questions = array();
foreach ($data as $questionskey => $questionsvalue) {
if ($quizvalue["quiz_id"] == $questionsvalue["quiz_id"] && !in_array($questionsvalue["question_id"], $question_id_list)) {
$answers = array();
array_push($question_id_list, $questionsvalue["question_id"]);
foreach ($data as $answerskey => $answersvalue) {
$myanswer = array();
if ($questionsvalue["question_id"] == $answersvalue["question_id"]) {
$myanswer["answer_id"] = $answersvalue["answer_id"];
array_push($answers, $myanswer);

}
}
$myquestion = array();
$myquestion["question_id"] = $questionsvalue["question_id"];
$myquestion["answers"] = $answers;
array_push($questions, $myquestion);

}
}
$myquiz = array();
$myquiz["quiz_id"] = $quizvalue["quiz_id"];
$myquiz["questions"] = $questions;
array_push($quizes, $myquiz);
}

}
return json_encode($quizes);

$data 的内容如下所示:

array (
0 =>array('quiz_id' => '1', 'question_id' => '1', 'answer_id' => '1',),
1 =>array('quiz_id' => '1', 'question_id' => '1', 'answer_id' => '2',),
...
5 =>array('quiz_id' => '1', 'question_id' => '2', 'answer_id' => '6',),
6 =>array('quiz_id' => '1', 'question_id' => '2', 'answer_id' => '7',),
...
22 =>array('quiz_id' => '2', 'question_id' => '6', 'answer_id' => '23',),
23 =>array('quiz_id' => '2', 'question_id' => '6', 'answer_id' => '24',),
)

结果应该返回一个如下所示的 json 对象:

[
{
"quiz_id": 1,
"questions": [
{"question_id":1, "answers": [{"answer_id":1}, {"answer_id":2}, {"answer_id":3},{"answer_id":4}]},
{"question_id":2, "answers": [{"answer_id":5}, {"answer_id":6}, {"answer_id":7},{"answer_id":8}]},
{"question_id":3, "answers": [{"answer_id":9}, {"answer_id":10}, {"answer_id":11},{"answer_id":12}]}
]
},
{
"quiz_id": 2,
"questions": [
{"question_id":4, "answers": [{"answer_id":13}, {"answer_id":14}, {"answer_id":15},{"answer_id":16}]},
{"question_id":5, "answers": [{"answer_id":17}, {"answer_id":18}, {"answer_id":19},{"answer_id":20}]},
{"question_id":6, "answers": [{"answer_id":21}, {"answer_id":22}, {"answer_id":23},{"answer_id":24}]}
]
}
]

最佳答案

你说这是 $TwoDimArray 的内容 没有什么可以用它做的。我怀疑还缺少一些东西。

array ( 'quiz_id' => '1', 'question_id' => '1', 'answer_id' => '1', ), 
array ( 'quiz_id' => '1', 'question_id' => '1', 'answer_id' => '2', ),
array ( 'quiz_id' => '1', 'question_id' => '1', 'answer_id' => '3', ),

这将摆脱你的“冗余数据”:

foreach($TwoDimArray as $value){
$quiz[$value['quiz_id']][$value['question_id']][$value['question_id']] = 1;
}

关于php - 在 PHP 中将二维数组转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com