1

更新

使用此代码,我可以检索差异,但我只能看到查询的第一个运动员。为什么?while 不应该循环 x 次,其中 x 是结果数?

function difference() {

global $db;

$result = $db->query("
        SELECT *
        FROM maxithlon
        WHERE owner = '". $_SESSION[teamid] ."'
        AND season = '". $this->season ."'
        AND week = '". $this->currentWeek ."'
        ORDER BY RAND();
");

while ($row = $result->fetch_assoc()) {

$result2 = $db->query("
        SELECT *
        FROM maxithlon
        WHERE owner = '". $_SESSION[teamid] ."'
        AND season = '". $this->season ."'
        AND week = '". $this->currentWeek ."-1'
        AND athleteId = '". $row[athleteId] ."'
");

while ($row2 = $result2->fetch_assoc()) {


    $difference[$row2[athleteId]][form] = $row[form] - $row2[form];
    $difference[$row2[athleteId]][maxid] = $row[maxid] - $row2[maxid];
    $difference[experience] = $row[experience] - $row2[experience];
    $difference[mood] = $row[mood] - $row2[mood];
    $difference[strenght] = $row[strenght] - $row2[strenght];
    $difference[stamina] = $row[stamina] - $row2[stamina];
    $difference[speed] = $row[speed] - $row2[speed];    
    $difference[agility] = $row[agility] - $row2[agility];  
    $difference[jump] = $row[jump] - $row2[jump];
    $difference[throws] = $row[throws] - $row2[throws];
    $difference[specialty1] = $row[specialty1] - $row2[specialty1];
    $difference[specialty2] = $row[specialty2] - $row2[specialty2];
    $difference[height] = $row[height] - $row2[height];
    $difference[weight] = $row[weight] - $row2[weight];
    $difference[fans] = $row[fans] - $row2[fans];
    $difference[wage] = $row[wage] - $row2[wage];

return($difference);
    }
    }

}

我一直在寻找一个可以满足我需求的答案,但我找不到它。

我有一个数据库,其中包含运动员数据的“maxithlon”表。这两个函数被定义为检索运动员的数据并将它们放入一个数组中。第一个检索上周的数据,第二个检索当前一周的数据。

我需要比较两个数组以获得两周的值之间的差异。你看到一个可能的解决方案吗?

4

3 回答 3

2

您可以做的是创建一个包含所有要查找值之间差异的键的数组。

$criteria = array('stamina', 'speed', 'agility', /*...*/);
$differences = array();
foreach($criteria as $key)
{
    $differences[$key] = $current[$key] - $last[$key];
}

假设 $current 和 $last 是包含有关给定运动员当前和前一周结果的数据的数组(因此您在评论中发布的数组的第二级)。

于 2012-12-11T15:17:14.407 回答
0

您可以使用array_diff(). 这是手册

于 2012-12-11T14:36:43.170 回答
0

如果满足您的需要,只需遍历数组并执行比较操作,因为没有直接功能

于 2012-12-11T14:45:42.933 回答