多维数组 - 按月分组 [英] Multidimensional Array- Grouping by month

查看:49
本文介绍了多维数组 - 按月分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力通过更多地使用多维数组来减少我必须进行的 SQL 查询的数量.问题是,他们对我来说是新的.有人可以给我一些指导如何做到这一点吗?

I'm trying to wade my way through working more with multidimensional arrays to decrease the number of SQL queries I'm having to make. The problem is, they are new to me. Can someone give me some direction how to accomplish this?

这是一个数组的例子:

Array
(
    [0] => Array
        (
            [0] => 2013-07-01
            [1] => Andy
            [2] => Hopkins Module
        )

    [1] => Array
        (
            [0] => 2013-07-01
            [1] => Frank
            [2] => Rotation
        )

    [2] => Array
        (
            [0] => 2013-07-01
            [1] => James
            [2] => Morning Report
        )

    [3] => Array
        (
            [0] => 2013-08-01
            [1] => James
            [2] => Noon Conference
        )

这个数组继续有更多的名字和月份.数据按名称排序,这意味着所有 James 项目都列在一个组中.每组名称可能有一个或多个数据点.我想做的是每个月循环并打印出第二个和第三个值.我知道如何使用 while 语句打印出所有内容,但我不确定如何按月对所有数据进行分组.

This array continues on with a lot more names and months. The data is ordered by name, meaning all of James item's are listed in a group. Each group of names may have one or more data points. What I would like to do is loop through each month and print out the second and third values. I know how to do a while statement to print out everything, but I'm not sure how to group all of the data by month.

推荐答案

试试这个(假设你展示的数组被分配给变量 $old_array):

Give this a try (it assumes the array you showed is assigned to the variable $old_array):

foreach ($old_array as $key => $value) {
  $new_array[$value[0]][] = array($value[1], $value[2]);
}
// Uncomment the next line to print the new array
// print_r($new_array);

这篇关于多维数组 - 按月分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆