【问题标题】:show Duplicate values in array显示数组中的重复值
【发布时间】:2013-11-26 00:07:46
【问题描述】:

我在尝试修改此代码以使其显示重复数据时遇到问题。 现在数组 $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
    )

)

【问题讨论】:

标签: php mysql arrays


【解决方案1】:
[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
                )

        )

【讨论】:

  • 我需要此信息来填充我的表单。但正如你所说,它们是唯一的键 ['gameID']。我认为除了重组数据之外,还有一种简单的方法。
  • 添加了一个更改,也许会有所帮助
  • 虽然这看起来不错,但是它修改了很多$playerPicks,其他页面上的其余代码无法正确读取。我还在尝试。
  • 我接受了您的回答,因为您基本上回答了我的要求,尽管结果并没有像我预期的那样工作。我确定如果我对数组的基本工作原理有更基本的了解,那么弄清楚这将是一件轻而易举的事。
  • 谢谢,很抱歉给您带来这么多麻烦。我确实找到了 Jeffrey Way 的一些视频,他是一位非常好的老师,在 tutsplus.com 上有很多好视频。它们可能会变得有用:tutsplus.com/lesson/arrays-part-1 & tutsplus.com/lesson/arrays-part-2
猜你喜欢
  • 1970-01-01
  • 2014-05-02
  • 2010-11-27
  • 2014-11-17
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多