2014-11-05 42 views
0

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

下面的代码:

  $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数组添加一行并将其设置为启动al值读取了查询,所以我得到很多重复的编码索引。

我怀疑它是usort。就像在排序之后,它不能再找到正确的元素来递增。

的是的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”如何随时间变化。我保存了'iPos'的值,并且上面的整个内容都在另一个循环中,以获得我感兴趣的日期。

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

回答

0

好吧,我是对的......这是usort。

我应该一直在使用uasort。

现在排序。