Php 如何获取数组中三个类别的总分百分比?
我有这样的回答Php 如何获取数组中三个类别的总分百分比?,php,arrays,laravel,Php,Arrays,Laravel,我有这样的回答 [0] => Array ( [category_id] => 2 [name] => Listening [question_id] => 10 [question_choice_id] => 38 [choice_level] => 0 ) [1] => Array ( [category_id] => 2 [name] => Listening [q
[0] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 10
[question_choice_id] => 38
[choice_level] => 0
)
[1] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 11
[question_choice_id] => 44
[choice_level] => 0
)
[2] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 12
[question_choice_id] => 45
[choice_level] => 0
)
[3] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 13
[question_choice_id] => 50
[choice_level] => 0
)
[4] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 14
[question_choice_id] => 55
[choice_level] => 0
)
[5] => Array (
[category_id] => 2
[name] => Listening
[question_id] => 15
[question_choice_id] => 58
[choice_level] => 1
)
[6] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 1
[question_choice_id] => 2
[choice_level] => 0
)
[7] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 2
[question_choice_id] => 8
[choice_level] => 0
)
[8] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 3
[question_choice_id] => 9
[choice_level] => 0
)
[9] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 4
[question_choice_id] => 13
[choice_level] => 1
)
[10] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 5
[question_choice_id] => 19
[choice_level] => 0
)
[11] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 6
[question_choice_id] => 22
[choice_level] => 1
)
[12] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 7
[question_choice_id] => 25
[choice_level] => 1
)
[13] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 8
[question_choice_id] => 30
[choice_level] => 0
)
[14] => Array (
[category_id] => 1
[name] => Reading
[question_id] => 9
[question_choice_id] => 36
[choice_level] => 0
)
[15] => Array (
[category_id] => 3
[name] => Grammar
[question_id] => 31
[question_choice_id] => 122
[choice_level] => 0
)
[16] => Array (
[category_id] => 3
[name] => Grammar
[question_id] => 32
[question_choice_id] => 125
[choice_level] => 0
)
因此,这里我有3个类别,即听力、阅读、语法
获取使用一个类别的用户的总百分比很容易,但我不知道如何获取并将这3个类别存储在我的数组中以显示给我的视图
我想用公式得到用户的百分比
分数=正确答案/总计数数组*100
正确答案在choice\u level
列中的值为1
我的例子是,公式应该是
Reading
TOTAL = 2 / 9 * 100
如何从这个数组中获取总数
一旦我得到答案,我只想让他们回到我的观点
我还有一个会话,它是category_id,它是一个数组,我想我可以用它来获得每个类别的结果
$category_id = [1,2,3];
这是我的数据库查询,我能在这里得到结果吗?而不是在我的后端编码
SELECT answer.category_id,category.name,answer.question_id,answer.question_choice_id,choice.choice_level
FROM answers AS answer
LEFT JOIN categories AS category ON answer.category_id = category.id
LEFT JOIN question_choices AS choice ON answer.question_choice_id = choice.id
WHERE answer.user_set_id = 1 and answer.user_id = 3
有人能帮我做这件事吗?我真的不知道该怎么办。如果有任何帮助,我们将不胜感激。其中一种方法是使用收集的方法
$original_array = collect([]); // your array
$expected_array = [];
$grouped_array = $original_array->groupBy('name')->map(function ($item, $key)
use (&$expected_array) {
$expected_array[$key]['occurence'] = $item->where('choice_level', 1)->count();
$expected_array[$key]['total'] = $item->count();
$expected_array[$key]['percentage'] = ($expected_array[$key]['occurence'] / $expected_array[$key]['total']) * 100;
});
return view('your-view', compact('expected_array'));
为什么不从mysql本身获取这些信息呢?那很容易,先生,但我做不到。这就是为什么我只是想通过我的回答来解决这个问题:(我应该发布我的查询和表吗,先生?是的,共享表架构,会给出一个查询。@JitendraYadav嗨,先生,我已经发布了我的查询。哦,这解决了我的问题。谢谢你,先生。我可以问先生吗?我如何将其存储到变量中?这样我就可以把它放在我的视图中?谢谢你的帮助!我真的很感激。嗨,我是真的假设我得到了错误的百分比答案,我认为
计数($value)
没有得到正确答案,在选择级别
列中的值为1,您能再看一看吗,先生?我想这不算正确答案。是的,先生!这就是我所做的,对不起,先生,我没有首先理解代码,我只是对我遇到的问题感到紧张。谢谢先生,我今天学到了一个新东西,
public function more_details(){
$category_id = Session::get('category_id'); //[1,2,3];
//more_details score of the user.
$score = Answer::get_user_more_details();
return view('pages.user.user_more_details', [
'title' => '',
]);
}
SELECT answer.category_id,category.name,answer.question_id,answer.question_choice_id,choice.choice_level
FROM answers AS answer
LEFT JOIN categories AS category ON answer.category_id = category.id
LEFT JOIN question_choices AS choice ON answer.question_choice_id = choice.id
WHERE answer.user_set_id = 1 and answer.user_id = 3
Category_table
id(PK) | name
1 Reading
2 Listening
3 Grammar
Answer_table
id(PK) | category_id | question_id | question_choice_id
1 2 10 38
2 2 11 44
3 2 12 45
4 2 13 50
5 2 14 55
6 2 15 58
7 1 1 2
8 1 2 8
9 1 3 9
10 1 4 13
11 1 5 19
12 1 6 22
13 1 7 25
14 1 8 30
15 1 9 36
16 3 31 122
17 3 32 125
Question_choice_table
id(PK) | choice_level
38 0
44 0
45 0
50 0
55 0
58 1
2 0
8 0
9 0
13 1
19 0
22 1
25 1
30 0
36 0
122 0
125 0
$original_array = collect([]); // your array
$expected_array = [];
$grouped_array = $original_array->groupBy('name')->map(function ($item, $key)
use (&$expected_array) {
$expected_array[$key]['occurence'] = $item->where('choice_level', 1)->count();
$expected_array[$key]['total'] = $item->count();
$expected_array[$key]['percentage'] = ($expected_array[$key]['occurence'] / $expected_array[$key]['total']) * 100;
});
return view('your-view', compact('expected_array'));