我有一个由代码索引的数组,但当我试图向其中一个元素添加值时,它没有。相反,每次循环时,它都会添加到另一行,就像没有找到编码索引一样。
这是代码:
$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数组中添加一行,并将它们设置为从查询中读取的初始值,所以我得到了很多重复的编码索引。
我怀疑是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的原因是我已经完成了上面的代码,但只调用了usort一次,而且它似乎有效,因为它确实增加了元素。
好吧,我是对的。。。是usort。
我本应该使用uasort的。
现在已排序。