PHP 中数组未定义索引错误(注意)
我有这个函数:
function coin_matrix($test, $revs) {
$coin = array();
for ($i = 0; $i < count($test); $i++) {
foreach ($revs as $j => $rev) {
foreach ($revs as $k => $rev) {
if ($j != $k &&
$test[$i][$j] != null &&
$test[$i][$k] != null) {
$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
}
}
}
}
return $coin;
}
问题
$test = array(
array('3'=>'1','5'=>'1'),
array('3'=>'2','5'=>'2'),
array('3'=>'1','5'=>'2'),
array('3'=>'1','5'=>'1'));
是
$revs = array('3'=>'A','5'=>'B');
,当我运行它时,它返回这些错误(通知):
注意:未定义的索引:第 10 行的 1
注意:第 10 行的未定义的索引:1
注意:未定义的索引:第 2 行的 2 10
注意:第 10 行未定义索引:2
注意:第 10 行未定义索引:2
注意:第 10 行未定义索引:1 即
这一行:$coin[$test[$i][$j]] [$test[$i][$k]] += 1 / ($some_var - 1);
问题是最后函数返回正确的矩阵(数组),如果我测试是否$coin[$test[$i][$j]][$test[$i][$k]]
存在,则不再返回它。
任何建议将不胜感激!
谢谢!
I have this function:
function coin_matrix($test, $revs) {
$coin = array();
for ($i = 0; $i < count($test); $i++) {
foreach ($revs as $j => $rev) {
foreach ($revs as $k => $rev) {
if ($j != $k &&
$test[$i][$j] != null &&
$test[$i][$k] != null) {
$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
}
}
}
}
return $coin;
}
where
$test = array(
array('3'=>'1','5'=>'1'),
array('3'=>'2','5'=>'2'),
array('3'=>'1','5'=>'2'),
array('3'=>'1','5'=>'1'));
and
$revs = array('3'=>'A','5'=>'B');
the problem is that when I run it, it returns these errors (notices):
Notice: Undefined index: 1 at line 10
Notice: Undefined index: 1 at line 10
Notice: Undefined index: 2 at line 10
Notice: Undefined index: 2 at line 10
Notice: Undefined index: 2 at line 10
Notice: Undefined index: 1 at line 10
which is this line: $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
The problem is that at the end the function returns the correct matrix (array) and if I test to see if $coin[$test[$i][$j]][$test[$i][$k]]
exists, then it doesn't return it anymore.
Any suggestion is greatly appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该警告是由
+=
生成的。+=
需要在添加元素之前查找该元素,并且您第一次访问$coin
中的任何元素时都没有初始化它们。The warning is being generated by the
+=
.+=
needs to look up the element before adding to it, and you haven't initialized any of the elements in$coin
the first time you access them.您可以/应该进行测试以确保在增加值之前设置
$coin[$test[$i][$j]][$test[$i][$k]]
。这不会改变代码的功能,但会使通知消失(这是一个很好的做法)。You can/should test to make sure that
$coin[$test[$i][$j]][$test[$i][$k]]
is set before incrementing the value. This shouldn't change the functionality of your code, but will make the notices go away (which is good practice).我认为问题是你试图使用 $coin 作为二维数组。
如果你希望它是二维的, $coin 必须是数组的数组。
I think the problem is you are trying to use $coin as a two dimensional array.
if you want it to be two dimensional, $coin has to be an array of arrays.
我不太明白,但我可以建议你使用
I don't understand much but I can suggest you to use
您是否考虑过将 for 循环替换为 foreach 循环?
例如:
这样做的好处是在 $test[ $i ] 中有一个值。然后,在您的
$test[ $i ][ $j ] == null
块中,放置以下内容:Have you thought about swapping out the for loop with a foreach loop?
Eg:
This has the benefit of having a value in $test[ $i ]. Then, in your
$test[ $i ][ $j ] == null
block, place this: