1

嗨,我有一组数据库结果,我需要将它们组织成一个多维数组,这样我就可以通过一封电子邮件而不是多封电子邮件向用户发送他们的结果。

我从数据库中获得的值是名称、电子邮件、route_name、route_details。理想情况下,我想根据用户电子邮件将路线分组在一起,因为这是他们的唯一标识符。我认为以下内容对我有用。

    Array
    (
       [email@email.com] => Array
                          (
                            [0] => name
                            [1] => Array
                                 (
                                   [0] => route_name
                                   [1] => route_details
                                 )
                             [2] => Array
                                 (
                                   [0] => route_name
                                   [1] => route_details
                                 )
                           )
       [email2@email2.com] => Array
                          (
                            [0] => name
                            [1] => Array
                                 (
                                   [0] => route_name
                                   [1] => route_details
                                 )
                            [2] => Array
                                 (
                                   [0] => route_name
                                   [1] => route_details
                                 )
                            [3] => Array
                                 (
                                   [0] => route_name
                                   [1] => route_details
                                 )

                           )
    )

任何帮助将不胜感激我在我的结果循环中尝试了以下内容,但我不确定如何根据电子邮件对结果进行分组,我不认为这正是我所需要的。

    $routeArray = array($data->email ,array($data->route_name, $data->route_details));

    $userEmailArray[] = $flightArray;

谢谢

4

1 回答 1

0

我认为以下对你有用。此代码将添加键名而不是数字索引。

// Add order by email clause
$sql = "select name, email, route_name, route_details from YOUR SQL QUERY ORDER BY    email";
$res = mysql_query($sql);

$desired_format = array();

while($row = mysql_fetch_assoc($res)){
    $tmp = array('route_name'=>$row['route_name'],'route_details'=>$row['route_details']);
    $desired_format[$row['email']]['name'] = $row['name'];
    $desired_format[$row['email']][] = $tmp;
}
于 2012-05-10T11:11:46.373 回答