我已经为MySQL提出了这个问题,但我现在想对 PHP 数组做同样的事情。
|--------|------------|--------|
| Name | Date | Points |
|--------|------------|--------|
| Tom | 2013-05-19 | 23 |
| Tom | 2013-05-20 | 11 |
| Tom | 2013-05-21 | 40 |
| Jack | 2013-05-19 | 34 |
| Jack | 2013-05-20 | 15 |
| Jack | 2013-05-21 | 28 |
| Arthur | 2013-05-19 | 9 |
| Arthur | 2013-05-20 | 12 |
| Arthur | 2013-05-21 | 42 |
| Bill | 2013-05-19 | 27 |
| Bill | 2013-05-20 | 23 |
| Bill | 2013-05-21 | 19 |
| ... | ... | ... |
对于一个给定的人,我想知道他先得多少次,最后得多少次。
我尝试了几件这样的事情:
<?php
$req = $bdd->prepare('SELECT Name, Date, Points FROM quotes WHERE Date > ? AND Date < ?');
$req->execute(array($_GET['datemin'], $_GET['datemax']));
$retour=array();
while ($donnees = $req->fetch(PDO::FETCH_ASSOC))
{
$retour[] = array('Date' => $donnees[Date], array(
'Name' => $donnees[Name],
'Points' => (float)$donnees[Points]
));
// $key = array_search($donnees[Date], $retour);
// var_dump($key);
/*
$retour[$i][] = array(
'Name' => $donnees[Name],
'Points' => (float)$donnees[Points]
);
*/
}
$nb_first=0;
$nb_last=0;
// Input name : $_GET['id']
/*
foreach ($retour as $key_n1 => $value_n1) // Loop for each date
{
foreach ($value_n1 as $key_n2 => $value_n2) // For an unique day, compare the people's results
{
// Test here ?
}
}
*/
$send_value = array(
'Nb_First' => $nb_first,
'Nb_Last' => $nb_last
);
// var_dump($send_value);
echo json_encode($send_value);
?>
我的想法是通过按日期“分组”来使用 3 维数组:
[01]
'Date' => '2013-05-19'
| [01]
| 'Name' => 'Tom'
| 'Points' => '23'
| [02]
| 'Name' => 'Jack'
| 'Points' => '34'
| [03]
| 'Name' => 'Arthur'
| 'Points' => '9'
| [04]
| 'Name' => 'Bill'
| 'Points' => '27'
[02]
'Date' => '2013-05-20'
| [01]
| 'Name' => 'Tom'
| 'Points' => '11'
| [02]
| 'Name' => 'Jack'
| 'Points' => '15'
[...]
然后,对于每个日期,尝试找出给定的人 ( $_GET['id']
) 是否具有最大值 (then $nb_first++;
) 或最小值 (then $nb_last++;
)。
也许有一些 PHP 函数可以帮助做到这一点。使用 SQL 查询更容易做到这一点。:-(