如何在PHP foreach循环中对数据库中的记录进行分组,然后将值合并到一个新数组中?

如何在PHP foreach循环中对数据库中的记录进行分组,然后将值合并到一个新数组中?

问题描述:

I have a table in a MySQL database that is used to store games that someone can book on. The games can be held in more than one location but they can also share the same time as another game. Here is an example of the data in the array that is taken from the MySQL database:

[0] => Array
    (
        [id] => 174
        [gamedatetime] => DateTime Object
            (
                [date] => 2016-02-08 14:00:00.000000
                [timezone_type] => 3
                [timezone] => Europe/London
            )

        [available] => 1
        [gameID] => 1
        [isBooked] => 
    )

[1] => Array
    (
        [id] => 175
        [gamedatetime] => DateTime Object
            (
                [date] => 2016-02-08 14:00:00.000000
                [timezone_type] => 3
                [timezone] => Europe/London
            )

        [available] => 1
        [gameID] => 1
        [isBooked] => 1
    )

[2] => Array
    (
        [id] => 176
        [gamedatetime] => DateTime Object
            (
                [date] => 2016-02-08 15:00:00.000000
                [timezone_type] => 3
                [timezone] => Europe/London
            )

        [available] => 1
        [gameID] => 1
        [isBooked] => 
    )

What I need to do is create an array with this data on it, but grouped by the date and time, and the gameID are joined together but comma separated, like this:

[0] => Array
    (
        [gamedatetime] => 2016-02-08 14:00:00.000000
        [id] => 174,175
    )
[1] => Array
    (
        [gamedatetime] => 2016-02-08 15:00:00.000000
        [id] => 176
    )

How can I achieve this using PHP?

我在MySQL数据库中有一个表,用于存储有人可以预订的游戏。 游戏可以在多个位置举行,但也可以与另一个游戏共享同一时间。 以下是从MySQL数据库中获取的数组中的数据示例: p>

  [0] => 数组
(
 [id] => 174 
 [gamedatetime] =>日期时间对象
(
 [日期] => 2016-02-08 14:00:00.000000 
 [timezone_type] =  > 3 
 [timezone] =>欧洲/伦敦
)
 
 [可用] => 1 
 [gameID] => 1 
 [isBooked] => 
)
  
 [1] => 数组
(
 [id] => 175 
 [gamedatetime] =>日期时间对象
(
 [日期] => 2016-02-08 14:00:00.000000 
 [timezone_type] =  > 3 
 [timezone] =>欧洲/伦敦
)
 
 [可用] => 1 
 [gameID] => 1 
 [isBooked] => 1 
)\  n 
 [2] => 数组
(
 [id] => 176 
 [gamedatetime] =>日期时间对象
(
 [日期] => 2016-02-08 15:00:00.000000 
 [timezone_type] =  > 3 
 [timezone] =>欧洲/伦敦
)
 
 [可用] => 1 
 [gameID] => 1 
 [isBooked] => 
)
   code>  pre> 
 
 

我需要做的是创建一个包含此数据的数组,但按日期和时间分组,并将gameID连接在一起,但以逗号分隔,如下所示 : p>

  [0] => 数组
(
 [gamedatetime] => 2016-02-08 14:00:00.000000 
 [id] => 174,175 
)
 [1] => 数组
(
 [gamedatetime] => 2016-02-08 15:00:00.000000 
 [id] => 176 
)
  code>  pre> 
 
 

我如何使用PHP实现这一目标? p> div>

The following will achieve what you are aiming for but the ids will be in a subarray, allowing you to concatenate together as you need them.

$newArray = [];
foreach ($oldArray as $game) {
    $newArray[$game['gamedatetime']]['gamedatetime'] = $game['gamedatetime'];
    $newArray[$game['gamedatetime']]['ids'][] = $game['id'];
}

or you can change the query to something like:

SELECT gamedatetime, GROUP_CONCAT(id) as `id`
FROM game
WHERE ...
GROUP BY gamedatetime

You would be more efficient by using the mysql GROUP BY word that would immediately return the array you want. If you just want to do it in php it's simply a matter of concatenate the results as you want them displayed which is not 100% clear to me in your question.

Try group_concat in query.

select gamedatetime, group_concat(`id` separator ',') as `id` from table group by gamedatetime;