从多维数组获取每个数组的平均值。

时间:2021-12-29 21:20:53

I have a multidimensional array of students 'name' and 'scores':

我有一个学生名字和分数的多维数组:

$student = array('Alice' => array(84, 93, 88, 100, 92, 84) ,
                 'bob' => array(92, 47, 68, 79, 89) , 
                 'charlie' => array(73, 85, 84, 69, 67, 92) , 
                 'denis' => array(59, 92, 83, 79, 73) , 
                 'eve' => array(91, 68, 85, 79, 84));

Now, I want to find the average of highest 'five' marks of each student:

现在,我想要找出每个学生最高的“五个”分数的平均值:

foreach ($students as $student => $key){
    echo $student . '<br>';

    arsort($key);

    $value   = array_slice($key, 0,5);

    foreach ($value as $output){
        $total    += $output . '<br />';

        $average   = $total / count($value);

    }
    echo $average . '<br/>';
}

My problem is, instead of giving the average of all the students, it is giving the average of only first student 'Alice'. What should I do to get the average of all students?

我的问题是,它没有给出所有学生的平均值,而是给出了只有第一个学生“爱丽丝”的平均值。我应该怎样做才能得到所有学生的平均成绩?

3 个解决方案

#1


0  

Several problems, but just replace your inner foreach() with:

有几个问题,但只要把你的内心替换成:

$average = array_sum($value) / count($value);

So:

所以:

foreach ($students as $student => $key){
    echo $student . '<br>';
    arsort($key);
    $value   = array_slice($key, 0,5);
    $average = array_sum($value) / count($value);
    echo $average . '<br/>';
}

#2


0  

If I understand the question correctly, the following code can be used to add each student's top 5 scores to an array, and then average that array.

如果我正确地理解了这个问题,下面的代码可以用来将每个学生的前5分添加到一个数组中,然后将该数组的平均值相加。

$scores = array();

foreach ($students as $student => $key){

     // Sort this student's scores
     arsort($key);

     // Add the top 5 scores to the scores array
     $scores = array_merge($scores, array_slice($key, 0,5));

}

// Average of all the top 5 scores
$average =  array_sum($scores) / count($scores);

#3


0  

There are three main problems with the approach you're currently taking.

你目前采取的方法有三个主要问题。

  1. You are overwriting the value of $average with each iteration of the loop
  2. 在循环的每次迭代中,您都在重写$平均值的值。
  3. You are calculating the average N times for each score
  4. 你计算每个分数的平均N次。
  5. You're incorrectly stating the average as SUM([score1...score5]) / N
  6. 你不正确地把平均值写成和([score1…score5]) / N。

Here is the correct implementation of the average of the top 5 scores of each student:

以下是每个学生的前5分的平均成绩:

$students = [
    'Alice'   => [84, 93, 88, 100, 92, 84],
    'bob'     => [92, 47, 68, 79, 89],
    'charlie' => [73, 85, 84, 69, 67, 92],
    'denis'   => [59, 92, 83, 79, 73],
    'eve'     => [91, 68, 85, 79, 84],
];

$averages = array_map(function($scores) {
    arsort($scores);
    return array_sum(array_slice($scores, 0, 5)) / 5;
}, $students);

var_dump($averages);

/* this gives us something like ... 
array(5) {
  ["Alice"]=>
  float(91.4)
  ["bob"]=>
  int(75)
  ["charlie"]=>
  float(80.6)
  ["denis"]=>
  float(77.2)
  ["eve"]=>
  float(81.4)
}
*/

Notice that to say $average = array_sum(array_slice($scores, 0, 5)) / count($scores) is actually incorrect, since you are only averaging the top 5 scores, you need not divide by count($scores) but by 5 instead.

注意,说$average = array_sum(array_slice($score, 0, 5)) / count($score)实际上是不正确的,因为您只平均前5个分数,所以您不需要除以count($score),而是用5来代替。

#1


0  

Several problems, but just replace your inner foreach() with:

有几个问题,但只要把你的内心替换成:

$average = array_sum($value) / count($value);

So:

所以:

foreach ($students as $student => $key){
    echo $student . '<br>';
    arsort($key);
    $value   = array_slice($key, 0,5);
    $average = array_sum($value) / count($value);
    echo $average . '<br/>';
}

#2


0  

If I understand the question correctly, the following code can be used to add each student's top 5 scores to an array, and then average that array.

如果我正确地理解了这个问题,下面的代码可以用来将每个学生的前5分添加到一个数组中,然后将该数组的平均值相加。

$scores = array();

foreach ($students as $student => $key){

     // Sort this student's scores
     arsort($key);

     // Add the top 5 scores to the scores array
     $scores = array_merge($scores, array_slice($key, 0,5));

}

// Average of all the top 5 scores
$average =  array_sum($scores) / count($scores);

#3


0  

There are three main problems with the approach you're currently taking.

你目前采取的方法有三个主要问题。

  1. You are overwriting the value of $average with each iteration of the loop
  2. 在循环的每次迭代中,您都在重写$平均值的值。
  3. You are calculating the average N times for each score
  4. 你计算每个分数的平均N次。
  5. You're incorrectly stating the average as SUM([score1...score5]) / N
  6. 你不正确地把平均值写成和([score1…score5]) / N。

Here is the correct implementation of the average of the top 5 scores of each student:

以下是每个学生的前5分的平均成绩:

$students = [
    'Alice'   => [84, 93, 88, 100, 92, 84],
    'bob'     => [92, 47, 68, 79, 89],
    'charlie' => [73, 85, 84, 69, 67, 92],
    'denis'   => [59, 92, 83, 79, 73],
    'eve'     => [91, 68, 85, 79, 84],
];

$averages = array_map(function($scores) {
    arsort($scores);
    return array_sum(array_slice($scores, 0, 5)) / 5;
}, $students);

var_dump($averages);

/* this gives us something like ... 
array(5) {
  ["Alice"]=>
  float(91.4)
  ["bob"]=>
  int(75)
  ["charlie"]=>
  float(80.6)
  ["denis"]=>
  float(77.2)
  ["eve"]=>
  float(81.4)
}
*/

Notice that to say $average = array_sum(array_slice($scores, 0, 5)) / count($scores) is actually incorrect, since you are only averaging the top 5 scores, you need not divide by count($scores) but by 5 instead.

注意,说$average = array_sum(array_slice($score, 0, 5)) / count($score)实际上是不正确的,因为您只平均前5个分数,所以您不需要除以count($score),而是用5来代替。