【问题标题】:Array Undefined index error (notice) in PHPPHP中的数组未定义索引错误(通知)
【发布时间】:2011-02-10 05:12:27
【问题描述】:

我有这个功能:

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]] 是否存在,那么它就不再返回它了。

非常感谢任何建议!

谢谢!

【问题讨论】:

    标签: php arrays undefined-index


    【解决方案1】:
    $coin[$test[$i][$j]][$test[$i][$k]] += 1 / ($some_var - 1);
    

    += 正在生成警告。 += 需要在添加之前查找元素,并且您在第一次访问 $coin 时还没有初始化任何元素。

    【讨论】:

      【解决方案2】:

      您可以/应该测试以确保在增加值之前设置了$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;
      }
      

      【讨论】:

      • 如果我这样做,那么该功能将不再起作用。我应该提到最后它返回正确的矩阵(数组)
      • 它是怎么破的?您确定只设置未设置的变量吗?
      • 我已经扩展了代码,以便您可以看到上下文。很高兴为您提供帮助。
      【解决方案3】:

      我认为问题在于您试图将 $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;
      }
      

      【讨论】:

        【解决方案4】:

        我不太了解,但我可以建议你使用

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2015-12-05
            • 2012-09-26
            • 2012-03-20
            • 1970-01-01
            • 1970-01-01
            • 2011-12-04
            • 2017-03-26
            • 2011-02-21
            • 2016-08-17
            相关资源
            最近更新 更多