gpt4 book ai didi

php - 如何在 php 中以父子格式从 mysql 检索数据?

转载 作者:行者123 更新时间:2023-11-29 02:38:39 24 4
gpt4 key购买 nike

我有两个表,

question                  question choices

id question id question_id choice
1 aaaa 1 1 ccc
2 bbbb 2 1 ddd
3 1 eee

我使用 php 使用 left join 查询获取数据,当我打印数据时,它看起来像这样,

Array
(
[id] => 1
[question] => aaa
[question_id] => 1
[choice] => ccc
)
Array
(
[id] => 1
[question] => aaa
[question_id] => 1
[choice] => ddd
)
Array
(
[id] => 1
[question] => aaa
[question_id] => 1
[choice] => eee
)

但是我想显示类似的东西,

Array
(
[id] => 1
[question] => aaa,
array
(
[question_id] => 1
[choice] => ccc
)
array
(
[question_id] => 1
[choice] => ddd
)
)

我想在父表中显示子表数据,这样我就可以防止问题重复。

我如何使用 core php 实现它?

最佳答案

不需要使用join查询,单独fetch即可。

    //$questions; $question_choices;
$questions = array_column($questions,null,'id');
foreach($question_choices as $question_choice){
$questions[$question_choice["question_id"]][] = $question_choice;
}

关于php - 如何在 php 中以父子格式从 mysql 检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58427441/

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