尝试增加元素时,向PHP索引添加额外的行

时间:2014-11-05 12:26:58

标签: php arrays usort

我有一个由代码索引的数组,但是当我尝试向其中一个元素添加值时,它并没有。相反,每次循环我的循环时,它都会添加到另一行,就像找不到编码索引一样。

以下是代码:

            $aPos = array();
            while ($Pos = mysql_fetch_row($PosRes)) {
                $aPos[$Pos[2]]['t'] = $Pos[2];
                $aPos[$Pos[3]]['t'] = $Pos[3]; // I use this for sorting

                if ($Pos[0] > $Pos[1]) { 
                    $aPos[$Pos[2]]['w']++; 
                    $aPos[$Pos[3]]['l']++; 
                    $aPos[$Pos[2]]['pts'] = $aPos[$Pos[2]]['pts'] + 3;
                }
                else if ($Pos[0] < $Pos[1]) { 
                    $aPos[$Pos[3]]['w']++;
                    $aPos[$Pos[2]]['l']++;
                    $aPos[$Pos[3]]['pts'] = $aPos[$Pos[3]]['pts'] + 3;
                }
                else { 
                    $aPos[$Pos[2]]['d']++;
                    $aPos[$Pos[3]]['d']++;
                    $aPos[$Pos[2]]['pts']++;
                    $aPos[$Pos[3]]['pts']++;
                }
                $aPos[$Pos[2]]['f'] = $aPos[$Pos[2]]['f'] + $Pos[0];
                $aPos[$Pos[2]]['a'] = $aPos[$Pos[2]]['a'] + $Pos[1];
                $aPos[$Pos[2]]['gd'] = $aPos[$Pos[2]]['f'] - $aPos[$Pos[2]]['a'];
                $aPos[$Pos[3]]['f'] = $aPos[$Pos[3]]['f'] + $Pos[1];
                $aPos[$Pos[3]]['a'] = $aPos[$Pos[3]]['a'] + $Pos[0];
                $aPos[$Pos[3]]['gd'] = $aPos[$Pos[3]]['f'] - $aPos[$Pos[3]]['a'];

                var_dump($aPos[$Pos[2]]); echo "<br />";
                var_dump($aPos[$Pos[3]]); echo "<br />";
            }
            usort($aPos, 'poscomp');
            $iPos  = findpos($aPos,$PosChartTeam);

poscomp是我的排序程序:

function poscomp($x, $y) {
  if (($x['pts'] == $y['pts']) && ($x['gd'] == $y['gd']) && ($x['f'] == $y['f']) && ($x['w'] == $y['w']) && ($x['t'] == $y['t'])) { return 0; }
  else if ($x['pts'] > $y['pts']) { return -1; }
  else if ($x['pts'] < $y['pts']) { return 1; }
  else if ($x['gd'] > $y['gd'])   { return -1; }
  else if ($x['gd'] < $y['gd'])   { return 1; }
  else if ($x['f'] > $y['f'])     { return -1; }
  else if ($x['f'] < $y['f'])     { return 1; }
  else if ($x['w'] > $y['w'])     { return -1; }
  else if ($x['w'] < $y['w'])     { return 1; }
  else if ($x['t'] > $y['t'])     { return -1; }
  else if ($x['t'] < $y['t'])     { return 1; }
                             else { return 1; }
}

findpos找到元素的数值:

function findpos($z, $sPos) {
  $iA = 1;
  foreach ($z as $a) {
   if ($a['t'] == $sPos) { return $iA; }
   $iA++;
  }
}

它不是增加元素中的值,而是向aPos数组添加一个额外的行并将它们设置为从查询中读出的初始值,因此我获得了大量重复的编码索引

我怀疑它是用户。它就像排序后一样,无法再找到正确的增量元素。

var_dump表示正确设置了行(最初至少):

 array(6) { ["t"]=> string(5) "WORCC" ["d"]=> int(1) ["pts"]=> int(1) ["f"]=> int(2) ["a"]=> int(2) ["gd"]=> int(0) }
 array(6) { ["t"]=> string(5) "SBCEL" ["d"]=> int(1) ["pts"]=> int(1) ["f"]=> int(2) ["a"]=> int(2) ["gd"]=> int(0) }
 array(5) { ["t"]=> string(5) "STOCC" ["l"]=> int(1) ["f"]=> int(0) ["a"]=> int(1) ["gd"]=> int(-1) }
 array(6) { ["t"]=> string(5) "NORTF" ["w"]=> int(1) ["pts"]=> int(3) ["f"]=> int(1) ["a"]=> int(0) ["gd"]=> int(1) } 

我试图找到iPos&#39;随着时间而变化。我把iPos&#39;存储起来。值,以及上面的全部内容都在另一个获取我感兴趣的日期的循环中。

我怀疑usort的原因是我已经完成了上面的代码,但只调用了一次,它似乎工作,因为它确实增加了元素。

1 个答案:

答案 0 :(得分:0)

好的,我是对的......这是好事。

我应该一直在使用uasort。

现在排序。

相关问题