我正在使用嵌套數組創建2D矩陣。它很容易找到使用嵌套的foreach
循環的行中所有值的總和。但是,我無法弄清楚如何對每列進行總結。即:我想爲每個item
找到score
的總和。在2D矩陣中求和列值
有什麼建議嗎?謝謝!!! :)
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
);