twoarmtom
2 weeks ago
380
37
Laravel

Getting average scores from objects in a collection

Posted 2 weeks ago by twoarmtom

I have a collection of questions and values (scores), and I'd like to get the average of all values for a specific question, but I'm worried that saving them as an object has made this difficult. Is there a way to average these?

Here is an example of how the collection appears:

[
{
"Question One": "4",
"Question Two": "3",
"Question Three": "4",
"Questions Four": "2",
"Question Five": "1"
},
{
"Question One": "3",
"Question Two": "2",
"Question Three": "1",
"Questions Four": "3",
"Question Five": "3"
},
{
"Question One": "3",
"Question Two": "3",
"Question Three": "2",
"Questions Four": "4",
"Question Five": "2"
}
]

I want to get the average scores for Question One, the average scores for Question Two, etc.

Any help would be amazing. Thank you.

Please sign in or create an account to participate in this conversation.