我无法尝试修改此代码,因此会显示重复的数据。 现在数组$ 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
)
)
答案 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
)
)