我正在使用一个类对矩阵进行一些计算,在这种情况下:获取每列的总和。
总和输出是正确的,如果我隐藏通知,问题就解决了,但从逻辑上讲,我更喜欢更正。
在 PHP 5.3 中,我在这个函数中得到了一些通知:
Notice: Undefined offset: 0
Notice: Undefined offset: 0
Notice: Undefined offset: 1
脚本
function sum()
{
foreach($this->numbers as $i => $rows)
{
foreach($rows as $j => $number)
{
$this->sum[0][$j] += $number; //notices here
}
}
$the_sum = new matrix($this->sum, 1, $this->get_num_columns());
return $the_sum;
}
矩阵:
1 | 4
0.25 | 1
var_dump($this->numbers);
array
0 =>
array
0 => int 1
1 => float 4
1 =>
array
0 => float 0.25
1 => int 1
和
$this->get_num_columns() // 2
任何想法来纠正这些通知?
谢谢