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

注意: 未定義のインデックス: 10 行目の 2

注意: 未定義のインデックス: 10 行目の 2

注意: 未定義のインデックス: 10 行目の 2

注意: 未定義のインデックス: 10 行目で 1

この行は次のとおりです。$coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);

問題は、関数が最後に正しい行列 (配列) を返し、存在するかどうかをテスト$coin[$test[$i][$j]][$test[$i][$k]]すると、それが返されなくなることです。

どんな提案でも大歓迎です!

ありがとう!

4

5 に答える 5

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

警告はによって生成されてい+=ます。 +=要素を追加する前に要素を検索する必要があり$coin、最初に要素にアクセスしたときに要素を初期化していない。

于 2010-04-20T15:58:21.757 に答える
3

$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;
}
于 2010-04-20T15:52:00.060 に答える
1

問題は、$coin を 2 次元配列として使用しようとしていることだと思います。

2 次元にしたい場合、$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;
}
于 2010-04-20T15:59:05.627 に答える
0

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);
            }
        }
于 2010-04-20T16:17:33.837 に答える
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;
}
于 2010-04-20T15:50:37.037 に答える