0

我已经为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 查询更容易做到这一点。:-(

4

1 回答 1

0

假设您的数组类似于以下内容:

$arr = array(   1 => array( 'Name'      => 'Tom',
                    'Date'      => '2013-05-19',
                    'Points'    => '23'),
        2 => array( 'Name'      => 'Tom',
                    'Date'      => '2013-05-20',
                    'Points'    => '11'),
        3 => array( 'Name'      => 'Tom',
                    'Date'      => '2013-05-21',
                    'Points'    => '40'),
        4 => array( 'Name'      => 'Jack',
                    'Date'      => '2013-05-19',
                    'Points'    => '34'),
        5 => array( 'Name'      => 'Jack',
                    'Date'      => '2013-05-20',
                    'Points'    => '15'),
        6 => array( 'Name'      => 'Jack',
                    'Date'      => '2013-05-21',
                    'Points'    => '28'),
        7 => array( 'Name'      => 'Arthur',
                    'Date'      => '2013-05-19',
                    'Points'    => '9'),
        8 => array( 'Name'      => 'Arthur',
                    'Date'      => '2013-05-20',
                    'Points'    => '12'),
        9 => array( 'Name'      => 'Arthur',
                    'Date'      => '2013-05-21',
                    'Points'    => '42'),
        10 => array('Name'      => 'Bill',
                    'Date'      => '2013-05-19',
                    'Points'    => '27'),
        11 => array('Name'      => 'Bill',
                    'Date'      => '2013-05-20',
                    'Points'    => '23'),
        12 => array('Name'      => 'Bill',
                    'Date'      => '2013-05-21',
                    'Points'    => '19'));

您可以生成类似于您正在寻找的东西:

$dates = array();
foreach ($arr as $key => $value){
    if (isset($dates[$value['Date']]['high'])){
        if ($value['Points'] >= $dates[$value['Date']]['high']){
            $dates[$value['Date']]['first'] = $value['Name'];
            $dates[$value['Date']]['high'] = $value['Points'];
        }
        if ($value['Points'] <= $dates[$value['Date']]['low']){
            $dates[$value['Date']]['last'] = $value['Name'];
            $dates[$value['Date']]['low'] = $value['Points'];
        }
    } else {
        $dates[$value['Date']]['first'] = $value['Name'];
        $dates[$value['Date']]['high'] = $value['Points'];
        $dates[$value['Date']]['last'] = $value['Name'];
        $dates[$value['Date']]['low'] = $value['Points'];
    }
    $dates[$value['Date']]['stats'][] = array(  'Name'      => $value['Name'],
                                                'Points'    => $value['Points']);
}
于 2013-06-09T16:07:14.820 回答