尝试递增元素时向PHP索引添加额外行
我有一个由代码索引的数组,但当我尝试向其中一个元素添加值时,它没有。相反,每次在我的循环中,它都会添加到另一行,就像没有找到编码索引一样 代码如下:尝试递增元素时向PHP索引添加额外行,php,arrays,usort,Php,Arrays,Usort,我有一个由代码索引的数组,但当我尝试向其中一个元素添加值时,它没有。相反,每次在我的循环中,它都会添加到另一行,就像没有找到编码索引一样 代码如下: $aPos = array(); while ($Pos = mysql_fetch_row($PosRes)) { $aPos[$Pos[2]]['t'] = $Pos[2]; $aPos[$Pos[3]]['t'] = $Pos[3]
$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);
它不是增加元素中的值,而是向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) }
我试图找出“IPO”是如何随时间变化的。我存储了“ipo”值,上面的所有内容都在另一个循环中,该循环获取我感兴趣的日期
我怀疑usort的原因是我已经完成了上面的代码,但是只调用了usort一次,它似乎可以工作,因为它确实增加了元素 好吧,我是对的……是乌索特 我应该用uasort 现在分类了
function findpos($z, $sPos) {
$iA = 1;
foreach ($z as $a) {
if ($a['t'] == $sPos) { return $iA; }
$iA++;
}
}
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) }