PHP 中数组未定义索引错误(注意)

发布于 2024-08-29 12:52:52 字数 1168 浏览 3 评论 0原文

我有这个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清风疏影 2024-09-05 12:52:52
$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);

该警告是由 += 生成的。 += 需要在添加元素之前查找该元素,并且您第一次访问 $coin 中的任何元素时都没有初始化它们。

$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);

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.

多孤肩上扛 2024-09-05 12:52:52

您可以/应该进行测试以确保在增加值之前设置 $coin[$test[$i][$j]][$test[$i][$k]] 。这不会改变代码的功能,但会使通知消失(这是一个很好的做法)。

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) {

                    // new tests go here
                    if(!isset(coin[$test[$i][$j]])) 
                    {
                        coin[$test[$i][$j]] = array(); 
                    }
                    if(!isset(coin[$test[$i][$j]][$test[$i][$k]])) 
                    {
                        coin[$test[$i][$j]][$test[$i][$k]] = 0; 
                    }

                    $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
                }
            }
        }
    }
    return $coin;
}

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).

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) {

                    // new tests go here
                    if(!isset(coin[$test[$i][$j]])) 
                    {
                        coin[$test[$i][$j]] = array(); 
                    }
                    if(!isset(coin[$test[$i][$j]][$test[$i][$k]])) 
                    {
                        coin[$test[$i][$j]][$test[$i][$k]] = 0; 
                    }

                    $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
                }
            }
        }
    }
    return $coin;
}
你与昨日 2024-09-05 12:52:52

我认为问题是你试图使用 $coin 作为二维数组。

如果你希望它是二维的, $coin 必须是数组的数组。

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) {
                // add this.
                if ($coin[$test[$i][$j]] == null){
                    $coin[$test[$i][$j]] = array();
                }
                // end
                $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
                }
            }
        }
    }
    return $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.

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) {
                // add this.
                if ($coin[$test[$i][$j]] == null){
                    $coin[$test[$i][$j]] = array();
                }
                // end
                $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
                }
            }
        }
    }
    return $coin;
}
夏日浅笑〃 2024-09-05 12:52:52

我不太明白,但我可以建议你使用

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;
}

I don't understand much but I can suggest you to use

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;
}
杀お生予夺 2024-09-05 12:52:52

您是否考虑过将 for 循环替换为 foreach 循环?
例如:

foreach( $tests as $i => $test )

这样做的好处是在 $test[ $i ] 中有一个值。然后,在您的 $test[ $i ][ $j ] == null 块中,放置以下内容:

        if ($j != $k && 
            // I suspect that this will cause errors too.
            // Do yourself a favor and add this sanity check.
            isset( $test[$j] ) && $test[$j] != null && 
            isset( $test[$k] ) && $test[$k] != null) {

                $currentK = $test[$k];
                $currentJ = $test[$j];
                // Use debug lines if setting things directly won't work
                if( !isset( $coin[ $currentJ ] ) || !is_array( $coin[ $currentJ ] ) )
                {
                    // $coin[ $currentJ ] = array();
                    die( "<pre>$currentK $currentJ \n" .  print_r( $coin ) );
                }
                $currentCoin =& $coin[ $currentJ ];
                if( !isset( $currentCoin [ $currentK ] ) || 
                    !is_array( $currentCoin [ $currentK ] ) )
                {
                    // Just curious, but when doing these checks before,
                    // did you remember to assign a numeric value here?
                    // 
                    // $currentCoin[ $currentK ] = 0;
                    die( "<pre>$currentK $currentJ \n" .  print_r( $coin ) );
                }
                $currentCoin[ $currentK ] += 1 / ($some_var - 1);
            }
        }

Have you thought about swapping out the for loop with a foreach loop?
Eg:

foreach( $tests as $i => $test )

This has the benefit of having a value in $test[ $i ]. Then, in your $test[ $i ][ $j ] == null block, place this:

        if ($j != $k && 
            // I suspect that this will cause errors too.
            // Do yourself a favor and add this sanity check.
            isset( $test[$j] ) && $test[$j] != null && 
            isset( $test[$k] ) && $test[$k] != null) {

                $currentK = $test[$k];
                $currentJ = $test[$j];
                // Use debug lines if setting things directly won't work
                if( !isset( $coin[ $currentJ ] ) || !is_array( $coin[ $currentJ ] ) )
                {
                    // $coin[ $currentJ ] = array();
                    die( "<pre>$currentK $currentJ \n" .  print_r( $coin ) );
                }
                $currentCoin =& $coin[ $currentJ ];
                if( !isset( $currentCoin [ $currentK ] ) || 
                    !is_array( $currentCoin [ $currentK ] ) )
                {
                    // Just curious, but when doing these checks before,
                    // did you remember to assign a numeric value here?
                    // 
                    // $currentCoin[ $currentK ] = 0;
                    die( "<pre>$currentK $currentJ \n" .  print_r( $coin ) );
                }
                $currentCoin[ $currentK ] += 1 / ($some_var - 1);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文