在2D矩阵中对列值求和

时间:2011-09-10 20:07:54

标签: php arrays matrix

我使用嵌套数组来创建2D矩阵。使用嵌套的foreach循环很容易找到行中所有值的总和。但是,我无法弄清楚如何对每一列进行总结。即:我想找到每个score item的总和。

有什么建议吗?谢谢!!! :)

PS:请注意,数组中的某些单元格没有值。这些将被视为0。

行间总和

foreach($critics as $array) {
    foreach($array as $item => $score) {
        $row_sum += $score;
    }
}

嵌套数组

$critics['Lisa Rose'] = array(
                        'Lady in the water' => 2.5,
                        'Snakes on a plane' => 3.5,
                        'Just my luck' => 3.0,
                        'Superman returns' => 3.5,
                        'You, me and dupree' => 2.5,
                        'The night listener' => 3.0
                        );

$critics['Gene Seymour'] = array(
                            'Lady in the water' => 3.0,
                            'Snakes on a plane' => 3.5,
                            'Just my luck' => 1.5,
                            'Superman returns' => 5.0,
                            'You, me and dupree' => 3.5,
                            'The night listener' => 3.0
                            );

$critics['Michael Phillips'] = array(
                            'Lady in the water' => 2.5,
                            'Snakes on a plane' => 3.0,
                            'Superman returns' => 3.5,
                            'The night listener' => 4
                            );

$critics['Claudia Puig'] = array(
                            'Snakes on a plane' => 3.5,
                            'Just my luck' => 3.0,
                            'Superman returns' => 4.5,
                            'You, me and dupree' => 4.0,
                            'The night listener' => 2.5
                            );

$critics['Mick LaSalle'] = array(
                            'Lady in the water' => 3.0,
                            'Snakes on a plane' => 4.0,
                            'Just my luck' => 2.0,
                            'Superman returns' => 3.0,
                            'You, me and dupree' => 3.0,
                            'The night listener' => 2.0
                            );

$critics['Jack Matthews'] = array(
                            'Lady in the water' => 3.0,
                            'Snakes on a plane' => 4.0,
                            'Just my luck' => 2.0,
                            'Superman returns' => 3.0,
                            'You, me and dupree' => 3.5,
                            );

$critics['Toby'] = array(
                            'Snakes on a plane' => 4.5,
                            'Just my luck' => 1.0,
                            'Superman returns' => 4.0
                            );

3 个答案:

答案 0 :(得分:0)

使用array_keys()确定每位评论家评分的每部电影的名称。将该影片的值添加到具有与键相同的影片名称的主数组$scores中:

$scores = array();
foreach ($critics as $critic) {

    // Get the movies scored by this critic
    $movies = array_keys($critic);

    // Add each movie's score to the master list's score
    foreach ($movies as $movie) {
      // Add it to the score if the movie is already in the master list
      if (isset($scores[$movie])) { 
        $scores[$movie] += $critic[$movie];
      }
      // Otherwise initialize this movie in the master list
      else $scores[$movie] = $critic[$movie];
    }
}

输出:

print_r($scores);
Array
(
    [Lady in the water] => 14
    [Snakes on a plane] => 26
    [Just my luck] => 12.5
    [Superman returns] => 26.5
    [You, me and dupree] => 16.5
    [The night listener] => 14.5
)

答案 1 :(得分:0)

// loop through each critic
foreach($critics as $array){
    // loop through each film
    foreach(array_keys($array) as $film){
        // add the score to the film's entry in the films array (or create the entry)
        $films[$film] += $array[$film];
    }
}

print_r($films);

其输出如下:

Array
(
    [Lady in the water] => 14
    [Snakes on a plane] => 26
    [Just my luck] => 12.5
    [Superman returns] => 26.5
    [You, me and dupree] => 16.5
    [The night listener] => 14.5
)

答案 2 :(得分:0)

$scores = array();
foreach ($critics as $items) {
    foreach ($items as $item => $score) {
        if (isset($scores[$item]))
            $scores[$item] += $score;
        else
            $scores[$item] = $score;
    }
}