我正在为我的网站编写一个模块,它给出了测试结果的细分,我需要得到与此类似的内容,
测试标题
平均标记85%测试题目2
平均标记12%
我从数据库中获取的数组看起来像这样,
Array
(
[0] => Array
(
[result_id] => 11
[test_taken] => 2011-10-04 16:22:59
[mark] => 5
[retaken] => false
[tests_test_id] => 4
[test_title] => Website Development CSS Basics
[test_slug] => website-development-css-basics
[mark_needed] => 90
[retake] => Yes
[topic_id] => 402
[topics_topic_id] => 402
)
[1] => Array
(
[result_id] => 12
[test_taken] => 2011-10-04 16:30:02
[mark] => 50
[retaken] => false
[tests_test_id] => 5
[test_title] => Another Test
[test_slug] => another-test
[mark_needed] => 10
[retake] => No
[topic_id] => 402
[topics_topic_id] => 402
)
)
这是我目前的查询,
$this->db->select('results.result_id, results.test_taken, results.mark, results.retaken, results.tests_test_id, tests.test_title, tests.test_slug, tests.mark_needed, tests.retake, topics.topic_id, tests.topics_topic_id')
->from('results')
->join('tests', 'tests.test_id = results.tests_test_id', 'left')
->join('topics', 'topics.topic_id = tests.topics_topic_id', 'left');
$query = $this->db->get();
return $query->result_array();
我需要以某种方式将具有相同测试ID的所有结果分组,以便我可以使用标记键获得结果的平均值。
这可以在PHP中使用吗?
答案 0 :(得分:0)
这是你应该让数据库为你做的事情。有点像:
SELECT AVG(mark) FROM test_results GROUP BY tests_test_id
答案 1 :(得分:0)
创建一个新数组然后循环 - 随你添加?
$sumArray = array();
foreach ($myArray as $key=>$val) {
$sumArray[$key]+=$val;
}
print_r($sumArray['test_id']);