Group php array by date

2019-08-05 12:43发布

I have an array:

Array
(
    [0] => Array
        (
            [id] => 81
            [placed] => 2013-09-19 16:32:53
            [sub_total] => 786
        )

    [1] => Array
        (
            [id] => 80
            [placed] => 2013-09-19 16:32:06
            [sub_total] => 780
        )

    [2] => Array
        (
            [id] => 79
            [placed] => 2013-09-18 17:06:48
            [sub_total] => 786
        )

    [3] => Array
        (
            [id] => 78            
            [placed] => 2013-09-18 17:05:02
            [sub_total] => 756
        )

    [4] => Array
        (
            [id] => 77          
            [placed] => 2013-09-17 17:02:53
            [sub_total] => 786
        )

    [5] => Array
        (
            [id] => 76
            [placed] => 2013-09-16 17:02:53
            [sub_total] => 756
        )
)

Is it possible to group this data by date and summarize subtotal amount to get output array:

 Array
    (
        [0] => Array
            (
                [placed] => 2013-09-19
                [sub_total] => 786 + 780
            )

        [2] => Array
            (
                [placed] => 2013-09-18
                [sub_total] => 786 + 756
            )

        [3] => Array
            (
                [placed] => 2013-09-17 17:02:53
                [sub_total] => 786
            )

        [4] => Array
            (
                [placed] => 2013-09-16 17:02:53
                [sub_total] => 756
            )
    )

5条回答
混吃等死
2楼-- · 2019-08-05 13:17
$output=array();
foreach($yourArray as $values)
{
 $d=date("Y-m-d",strtotime($values["placed"]));
 $output[$d]["sub_total"]+=$values["sub_total"];
}

print_r($output);

Fiddle

Credits: The initial array used on this fiddle was taken from the answer by Jason OOO below.

查看更多
3楼-- · 2019-08-05 13:20

You can have that kind of array at querying time. Something like

select date_field_name,other_field from table_name group by Day(date_feild_name);

after that you can use a use foreach to work with each day's data!

查看更多
一纸荒年 Trace。
4楼-- · 2019-08-05 13:27

I tested this also: http://phpfiddle.org/main/code/rzv-ngp

<?php
Array
(
    '0' => Array
        (
            'id' => '81',
            'placed' => '2013-09-19 16:32:53',
            'sub_total' => '786'
        ),

    '1' => Array
        (
            'id' => '80',
            'placed' => '2013-09-19 16:32:06',
            'sub_total' => '780'
        ),

//...
);

$newarray = array();
foreach ($array as $value){
 $temp = explode(" ", $value['placed']);
 $date = $temp[0];
    $total = (isset($newarray[$date]['sub_total']) ? $newarray[$date]['sub_total'] + $value['sub_total']: $value['sub_total']);
 $newarray[$date] = array('placed' => $date, 'sub_total' => $total);
}

print_r($newarray);

?>
查看更多
啃猪蹄的小仙女
5楼-- · 2019-08-05 13:28
Try this code
<?php
$shop = array(array( id => '81', 
                      placed => '2013-09-19',
                      sub_total => '786' 
                    ),
             array( id =>'80', 
                      placed => '2013-09-19',
                      sub_total => '780',
                    ),
              array( id => '79', 
                      placed => '2013-09-18',
                      sub_total => '786' 
                    ),
              array
                        (
                    id => '78',            
                    placed => '2013-09-18',
                    sub_total => '756'
                 ),
            array(
                     id => '77',          
                    placed => '2013-09-17',
                    sub_total => '786'
            ),
            array(
                     id => '76',          
                    placed => '2013-09-16',
                    sub_total => '756'
            )
             );
$result=array();
foreach($shop as $value)
{
    if(!isset($result[$value['placed']]))
    {
        //echo $value['placed'];
        //echo $result[$value['placed']];
        $result[$value['placed']]=array('placed'=>$value['placed'],'sub_total'=>0);


    }
    $result[$value['placed']]['sub_total']+=$value['sub_total'];
}

print_r($result);
//print_r($shop);
?>
查看更多
Anthone
6楼-- · 2019-08-05 13:30

Try using sub query

     select field_name,DAY(date_field) as date,(select sum(sub_total) where DAY(date_field)=date) as sub_total from table_name group by day(date_field)
查看更多
登录 后发表回答