多维数组合并

时间:2014-03-11 13:27:13

标签: php mysql arrays multidimensional-array

我正在使用存储统计信息的mysql表,我使用以下查询检索特定时段的值:

SELECT t2.NAME, COUNT(t1.TYPE) AS total, t1.DATE, t1.TYPE
        FROM stats AS t1
        LEFT JOIN agents AS t2 ON t1.AGENTID = t2.ID
        WHERE (DATE between  DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() ) 
        GROUP BY DATE, AGENT, TYPE

这将返回以下数组中的数据:

Array
(
    [0] => Array
        (
            [NAME] => Jack Blake
            [total] => 15
            [DATE] => 2014-03-03
            [TYPE] => 1
        )

    [1] => Array
        (
            [NAME] => Jack Blake
            [total] => 13
            [DATE] => 2014-03-03
            [TYPE] => 4
        )

    [2] => Array
        (
            [NAME] => John Doe
            [total] => 5
            [DATE] => 2014-03-03
            [TYPE] => 1
        )

    [3] => Array
        (
            [NAME] => John Doe
            [total] => 2
            [DATE] => 2014-03-03
            [TYPE] => 3
        )

    [4] => Array
        (
            [NAME] => John Doe
            [total] => 2
            [DATE] => 2014-03-03
            [TYPE] => 4
        )

    [5] => Array
        (
            [NAME] => Jen Jester
            [total] => 8
            [DATE] => 2014-03-03
            [TYPE] => 1
        )

    [6] => Array
        (
            [NAME] => Jen Jester
            [total] => 1
            [DATE] => 2014-03-03
            [TYPE] => 3
        )

    [7] => Array
        (
            [NAME] => Jen Jester
            [total] => 3
            [DATE] => 2014-03-03
            [TYPE] => 4
        )

    [8] => Array
        (
            [NAME] => Jane Doe
            [total] => 7
            [DATE] => 2014-03-03
            [TYPE] => 1
        )

    [9] => Array
        (
            [NAME] => Jane Doe
            [total] => 4
            [DATE] => 2014-03-03
            [TYPE] => 4
        )

    [10] => Array
        (
            [NAME] => Jack Blake
            [total] => 51
            [DATE] => 2014-03-04
            [TYPE] => 1
        )

    [11] => Array
        (
            [NAME] => Jack Blake
            [total] => 14
            [DATE] => 2014-03-04
            [TYPE] => 4
        )

    [12] => Array
        (
            [NAME] => John Doe
            [total] => 5
            [DATE] => 2014-03-04
            [TYPE] => 1
        )

    [13] => Array
        (
            [NAME] => John Doe
            [total] => 3
            [DATE] => 2014-03-04
            [TYPE] => 4
        )

    [14] => Array
        (
            [NAME] => Jen Jester
            [total] => 3
            [DATE] => 2014-03-04
            [TYPE] => 1
        )

    [15] => Array
        (
            [NAME] => Jen Jester
            [total] => 19
            [DATE] => 2014-03-04
            [TYPE] => 4
        )
)

我尝试了几种方法,但似乎仍然失败,我想通过SQL STATEMENT或PHP函数以下列方式返回数组:

Array 
(
    [2014-03-03] => Array
        (
            [0] => Array
                (
                    [NAME] => Jack Blake
                    [1] => 15
                    [4] => 13
                )
            [1] => Array
                (
                    [NAME] => John Doe
                    [1] => 5
                    [3] => 2
                    [4] => 2
                )
            [2] => Array
                (
                    [NAME] => Jen Jester
                    [1] => 8
                    [3] => 1
                    [4] => 3
                )
            [2] => Array
                (
                    [NAME] => Jane Doe
                    [1] => 7
                    [4] => 4
                )
        )
    [2014-03-04] => Array
        (
            [0] => Array
                (
                    [NAME] => Jack Blake
                    [1] => 51
                    [4] => 14
                )
            [1] => Array
                (
                    [NAME] => John Doe
                    [1] => 5
                    [4] => 3
                )
            [2] => Array
                (
                    [NAME] => Jen Jester
                    [1] => 3
                    [4] => 19
                )
        )
)

您会注意到每个键都是DATE,每个子键包含TYPE作为键,其值在同一DATE与相同的NAME键相关联。

我希望这很清楚,可以提供解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:1)

这将根据您的需要对您的列表进行排序

$arrays = array();
foreach ($results as $result) {
    $arrays[$result['DATE']][$result['NAME']]['NAME'] = $result['NAME'];
    $arrays[$result['DATE']][$result['NAME']][$result['TYPE']] = $result['total'];
}

$newArray = array();
foreach ($arrays as $key => $array) {
    $newArray[$key] = array_values($array);
}

$ newArray将包含所有格式化的元素,可能有一种更简单的方法,但这确实有效