如果有答案,请提前抱歉,但我找不到我正在寻找的东西。
我目前有一个报告应用程序,它将数据结果从数据库查询返回到多维数组中。每个数组都是根据 id 字段构建的。我希望能够将数组合并到一个 id 相同的数组中。
例如这里是一个测试结果集。如您所见,id 为 1 的用户当前有两个结果。
Array
(
[0] => Array
(
[AssessmentQuestion] => 1
[AssessmentAnswer] => 0
[id] => 1
)
[1] => Array
(
[AssessmentQuestion] => 2
[AssessmentAnswer] => 2
[id] => 1
)
)
Array
(
[0] => Array
(
[AssessmentQuestion] => 1
[AssessmentAnswer] => 1
[id] => 2
)
)
如果 id 相同,我希望结果集看起来像这样。
Array
(
[0] => Array
(
[AssessmentQuestion] => 1
[AssessmentAnswer] => 0
[AssessmentQuestion] => 2
[AssessmentAnswer] => 2
[id] => 1
)
)
提前非常感谢。
这是我正在运行的查询。有一个自定义函数 ArrayFunctions::captureArrayFromResult(),这只是捕获数组结果。第一个查询只获取所有活动用户条目 id,然后每个 id 返回所有评估问题信息。
/**** COLLECT ALL ACTIVE USER ENTRY IDS ****/
$query = "SELECT DISTINCT ue.id
FROM user_entry ue
LEFT JOIN user_demographic_answers uda ON ue.id = uda.user_entry_id
LEFT JOIN user_assessment_answers uaa ON ue.id = uaa.user_entry_id
WHERE $condition
AND DATE(ue.datetime_finished) BETWEEN '{$this->reportDateRangeStart}' AND '{$this->reportDateRangeEnd}'";
$results = db_Query($query);
$this->reportUserEntryID = ArrayFunctions::captureArrayFromResult($results, "id", "id");
foreach($this->reportUserEntryID as $userID)
{
/**** COLLECT ASSESSMENT QUESTIONS ****/
$query = SELECT DISTINCT uaa.question_id as 'AssessmentQuestion', uaa.answer_value as 'AssessmentAnswer', ue.id
FROM user_assessment_answers uaa
LEFT JOIN report_field_options rfo ON uaa.question_id = rfo.assessment_question_id
LEFT JOIN user_entry ue ON uaa.user_entry_id = ue.id
WHERE ($condition)
AND DATE(ue.datetime_finished) BETWEEN '{$this->reportDateRangeStart}' AND '{$this->reportDateRangeEnd}'
AND ue.id IN ($userID)
ORDER BY uaa.question_id";
$results = db_Query($query);
$this->reportQuestionAssessmentAnswers = ArrayFunctions::captureArrayFromResult($results);
}