php组查询结果AFTER查询

时间:2016-03-18 23:40:27

标签: php arrays multidimensional-array

我的选择查询($ sql ="选择日期,小时,美元来自db";)输出包含744行,每31小时一小时一行。我回应它们就像这样(可读性为逗号):

day 1, hour 1, dollars 300
day 1, hour 2, dollars 310
day 1, hour 3, dollars 299
/*etc., up to row 744, which looks like the following */
day 31, hour 24, dollars 200

所以,31天,每天24小时,全部744行。

在php中,我想按天分组行,以便我得到

da 1:
hour 1, dollars 300
hour 2, dollars 310
hour 3, dollars 299
..
hour 24, dollars 299
31:
24, 200

要将day设置为高于hourdollars的级别,我尝试制作一个数组,如下所示:

$arr[] = array( $day =>['hour' => $hour, 'dollars' => $dollars]);

print_r($arr)产生了这个:

Array
(
    [1] => Array
        (
            [hour] => 1
            [dollars] => 300
        )

)
Array
(
    [1] => Array
        (
            [hour] => 2
            [dollars] => 310
        )

)
Array
(
    [1] => Array
        (
            [hour] => 3
            [dollars] => 299
        )

)

// etc. up to:

Array
(
    [31] => Array
        (
            [hour] => 24
            [dolars] => 200
        )

)

......但我需要的是这样的事情:

Array
(
    [1] => 
        [
            [hour] => 1
            [dollars] => 300
        ],
        [
            [hour] => 2
            [dollars] => 310
        ],
        [
            [hour] => 3
            [dollars] => 299
        ],
/*etc., up to */
        [
            [hour] => 24
            [dollars] => 200
        ]
)

有关我如何才能这样做的任何建议?

1 个答案:

答案 0 :(得分:0)

试试这个

$arr[$day] []= array( 'hour' => $hour, 'dollars' => $dollars);