如何在PHP中合并问题和选项数组?
我对使用Laravel和处理数组中的数据仍然是新手 基本上我有两个需要合并的表 首先是问题,其次是选择 这是我的问题列表如何在PHP中合并问题和选项数组?,php,arrays,laravel,Php,Arrays,Laravel,我对使用Laravel和处理数组中的数据仍然是新手 基本上我有两个需要合并的表 首先是问题,其次是选择 这是我的问题列表 [0] => Array ( [id] => 1 [question] => What major problem are Texans having? ) [1] => Array ( [id] => 2 [question] => What did civic leaders suggest?
[0] => Array (
[id] => 1
[question] => What major problem are Texans having?
)
[1] => Array (
[id] => 2
[question] => What did civic leaders suggest?
)
[2] => Array (
[id] => 3
[question] => What is the author’s general intention in writing this article?
)
$questions = Question::select(
'question.id',
'question.component AS question',
)
->from('questions AS question')
->leftJoin('scenarios AS scenario', 'scenario.id', '=', 'question.scenario_id')
->where('question.scenario_id',1)
->get()
->toArray();
$choices = Question_choice::select(
'choice.question_id',
'choice AS choice'
)
->from('question_choices AS choice')
->leftJoin('questions AS question', 'question.id', '=', 'choice.question_id')
->where('question.id', ```looping the id data that i have in the question table```)
->get()
->toArray();
这是我的选择数组
[0] => Array (
[question_id] => 1
[choice] => gas shortage
)
[1] => Array (
[question_id] => 1
[choice] => panic buying
)
[2] => Array (
[question_id] => 1
[choice] => Hurricane Harvey
)
[3] => Array (
[question_id] => 1
[choice] => help from oil companies
)
[4] => Array (
[question_id] => 2
[choice] => gas shortage
)
[5] => Array (
[question_id] => 2
[choice] => panic buying
)
[6] => Array (
[question_id] => 2
[choice] => Hurricane Harvey
)
[7] => Array (
[question_id] => 2
[choice] => help from oil companies
)
[8] => Array (
[question_id] => 3
[choice] => gas shortage
)
[9] => Array (
[question_id] => 3
[choice] => panic buying
)
[10] => Array (
[question_id] => 3
[choice] => Hurricane Harvey
)
[11] => Array (
[question_id] => 3
[choice] => help from oil companies
)
我在考虑如何根据选择的问题组合这两个数组?因此,当我在视图中显示它时,它将非常容易。我不知道如何使用多维数组,我在这里寻求如何使用多维数组的帮助
下面是我目前获取数据的代码
这是用来回答问题的
[0] => Array (
[id] => 1
[question] => What major problem are Texans having?
)
[1] => Array (
[id] => 2
[question] => What did civic leaders suggest?
)
[2] => Array (
[id] => 3
[question] => What is the author’s general intention in writing this article?
)
$questions = Question::select(
'question.id',
'question.component AS question',
)
->from('questions AS question')
->leftJoin('scenarios AS scenario', 'scenario.id', '=', 'question.scenario_id')
->where('question.scenario_id',1)
->get()
->toArray();
$choices = Question_choice::select(
'choice.question_id',
'choice AS choice'
)
->from('question_choices AS choice')
->leftJoin('questions AS question', 'question.id', '=', 'choice.question_id')
->where('question.id', ```looping the id data that i have in the question table```)
->get()
->toArray();
这是为了得到问题的选择
[0] => Array (
[id] => 1
[question] => What major problem are Texans having?
)
[1] => Array (
[id] => 2
[question] => What did civic leaders suggest?
)
[2] => Array (
[id] => 3
[question] => What is the author’s general intention in writing this article?
)
$questions = Question::select(
'question.id',
'question.component AS question',
)
->from('questions AS question')
->leftJoin('scenarios AS scenario', 'scenario.id', '=', 'question.scenario_id')
->where('question.scenario_id',1)
->get()
->toArray();
$choices = Question_choice::select(
'choice.question_id',
'choice AS choice'
)
->from('question_choices AS choice')
->leftJoin('questions AS question', 'question.id', '=', 'choice.question_id')
->where('question.id', ```looping the id data that i have in the question table```)
->get()
->toArray();
我的想法是这样的
[0] => Array (
[id] => 1
[question] => What major problem are Texans having?
=>[choices] =>[0] gas shortage
[1] panic buying
[2] Hurricane Harvey
[3] help from oil companies
)
我不知道我是否讲得通,任何帮助都将不胜感激。我真的被我现在正在做的事情困住了。欢迎来到Stack Overflow 在拉威尔,你可以使用 当使用Eloquent时,您不需要编写自己的查询,Eloquent将管理关系 创建一个模型并使用
hasMany
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
public function choices()
{
return $this->hasMany('App\Model\Choice', 'question_id');
}
}
欢迎来到堆栈溢出 在拉威尔,你可以使用 当使用Eloquent时,您不需要编写自己的查询,Eloquent将管理关系 创建一个模型并使用
hasMany
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
public function choices()
{
return $this->hasMany('App\Model\Choice', 'question_id');
}
}
我建议使用集合来处理它。因此,如果我们假设您已经有一系列的问题和答案,下面的一段代码将满足您的需要。关键是它不需要对数据库进行任何查询,所有进程都将在应用程序层处理
$questions = [...]; // array of questions
$answers = [...]; // array of answers
$questionsCollection = new Collection($questions);
$answersCollection = new Collection($answers);
$groupedAnswers = $answersCollection->groupBy('question_id');
$questionAndAnswers = $groupedAnswers->map( function ($choices, $questionId) use ($questionsCollection) {
$question = $questionsCollection->where('id', $questionId)->first();
$question['choices'] = $choices;
return $question;
});
简要说明我在代码中做什么:
- 我正在使用所需的数组创建集合的实例。因此,我可以在使用收集方法时使用数组的数据
- 所有选项根据问题id分组在一起
- 通过使用“map”方法及其回调,我们可以迭代答案,并使用“where”方法和每次迭代中可用的问题id查找相关问题
我建议使用集合来处理它。因此,如果我们假设您已经有一系列的问题和答案,下面的一段代码将满足您的需要。关键是它不需要对数据库进行任何查询,所有进程都将在应用程序层处理
$questions = [...]; // array of questions
$answers = [...]; // array of answers
$questionsCollection = new Collection($questions);
$answersCollection = new Collection($answers);
$groupedAnswers = $answersCollection->groupBy('question_id');
$questionAndAnswers = $groupedAnswers->map( function ($choices, $questionId) use ($questionsCollection) {
$question = $questionsCollection->where('id', $questionId)->first();
$question['choices'] = $choices;
return $question;
});
简要说明我在代码中做什么:
- 我正在使用所需的数组创建集合的实例。因此,我可以在使用收集方法时使用数组的数据
- 所有选项根据问题id分组在一起
- 通过使用“map”方法及其回调,我们可以迭代答案,并使用“where”方法和每次迭代中可用的问题id查找相关问题
您好,先生,我试过了,但结果是,它获取了选项表中的所有数据,但我只想根据我在问题表数据中的id获取选项中的数据。关系用于获取结果,就像您稍后所说的那样。如果您正确使用它,在foreach中您将有疑问和它的选择,下一次迭代将有疑问和它的选择,等等……在控制器中,您可以像
question::with('choices')->get()代码>@Rinto George请帮我回答这个问题。您好,先生,我试过了,但输出是,它获取选项表中的所有数据,但我只想根据我在问题表数据中的id获取选项中的数据。关系用于获取结果,就像您稍后所说的那样。如果您正确使用它,在foreach中您将有疑问和它的选择,下一次迭代将有疑问和它的选择,等等……在控制器中,您可以像question::with('choices')->get()代码>@Rinto George请帮我回答这个问题。不要使用leftJoin
,尝试eagar加载
,这是laravel雄辩的方法。我建议@Rinto George answer不要使用leftJoin
,尝试eagar加载
,这是laravel雄辩的方法。我推荐@Rinto-George-answer