显示数组中的重复值

时间:2013-11-26 00:07:46

标签: php mysql arrays

我无法尝试修改此代码,因此会显示重复的数据。 现在数组$ playerPicks看起来像'数组A',我希望它从数据库中提取数据并显示为'数组B'。数据在数据库中。但不是像我想的那样表现出来。对于更高级的,我仍在学习,这可能是一件简单的事情。  谢谢。

代码

$sql = "select distinct weekNum from " . $db_prefix . "schedule order by weekNum;";
$query = mysql_query($sql);
$i = 0;
while ($result = mysql_fetch_array($query)) {
if ($i > 0) $weekNav .= ' | ';
if ($week !== (int)$result['weekNum']) {
    $weekNav .= '<a href="results.php?week=' . $result['weekNum'] . '">' . $result['weekNum'] . '</a>';
} else {
    $weekNav .= $result['weekNum'];
}
$i++;
}

$playerPicks = array();
$playerTotals = array();
$sql = "select p.userID, p.gameID, p.pickID ";
$sql .= "from " . $db_prefix . "picks p ";
$sql .= "inner join " . $db_prefix . "users u on p.userID = u.userID ";
$sql .= "inner join " . $db_prefix . "schedule s on p.gameID = s.gameID ";
$sql .= "where s.weekNum = " . $week . " and u.userName <> 'admin' ";
$sql .= "order by p.userID, s.gameTimeEastern, s.gameID";
$query = mysql_query($sql);
$i = 0;
while ($result = mysql_fetch_array($query)) {
$playerPicks[$result['userID']][$result['gameID']] = $result['pickID'];
if (!empty($games[$result['gameID']]['winnerID']) && $result['pickID'] == $games[$result['gameID']]['winnerID']) {
    //player has picked the winning team
    $playerTotals[$result['userID']] += 1;
} else {
    if ( $playerTotals[$result['userID']] += 0);
}
$i++;
}

echo '<pre>';
print_r($playerPicks);
echo '<pre>';

输出

Array A
(
[2] => Array
    (
        [433] => GB
    )

[924] => Array
    (
        [435] => PIT
    )

[934] => Array
    (
        [434] => OAK
    )

 )



Array B
(
[2] => Array
    (
        [433] => GB
        [433] => GB
    )

[924] => Array
    (
        [435] => PIT
        [435] => PIT
    )

[934] => Array
    (
        [434] => OAK
        [434] => OAK
    )

)

1 个答案:

答案 0 :(得分:0)

[2] => Array
    (
        [433] => GB
        [433] => GB
    )

这是不可能的。他们需要拥有唯一的密钥您需要找到一种更好的方法来构建数据。 为什么需要重复信息?

以下是如何保存数据的一个示例

[2] => Array
        (
            [433] => Array
                            (
                                [type] => GB
                                [amount] => 2
                            )
            [435] => Array
                            (
                                [type] => PIT
                                [amount] => 5
                            )
        )

编辑:

如果你改变

,那就不是那么大的变化了
$playerPicks[$result['userID']][$result['gameID']] = $result['pickID'];

$playerPicks[$result['userID']][$result['gameID']][] = $result['pickID'];

你会得到

[123] => Array
        (
            [456] => Array
                (
                    [0] => GB
                    [1] => GB
                )

            [454] => Array
                (
                    [0] => PIT
                )

        )