如果key相同,则组合数组并添加值

时间:2016-07-15 14:17:10

标签: php

标题几乎说明了一切。如果日期与具有相同项目ID的特定数组中的其他日期匹配,我正在尝试组合数组。所以例如有。这是我到目前为止所做的。

Array
(
    [1488] => Array
        (
            [project_name] => test
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-07-11 - 2016-07-17
                            [1] => 37
                        )

                    [1] => Array
                        (
                            [0] => 2016-07-04 - 2016-07-10
                            [1] => 51
                        )

                    [2] => Array
                        (
                            [0] => 2016-07-04 - 2016-07-10
                            [1] => 44
                        )

                    [3] => Array
                        (
                            [0] => 2016-07-04 - 2016-07-10
                            [1] => 22
                        )

                )

        )

    [2] => Array
        (
            [project_name] => Invoice 78554
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-06-13 - 2016-06-19
                            [1] => 10
                        )

                )

        )

    [3] => Array
        (
            [project_name] => Small Move*
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-06-27 - 2016-07-03
                            [1] => 3.5
                        )

                    [1] => Array
                        (
                            [0] => 2016-05-30 - 2016-06-05
                            [1] => 9.5
                        )

                )

        )

)

我希望它看起来像这样......

    Array
(
    [1488] => Array
        (
            [project_name] => test
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-07-11 - 2016-07-17
                            [1] => 37
                        )

                    [1] => Array
                        (
                            [0] => 2016-07-04 - 2016-07-10
                            [1] => 117
                        )

                )

        )

    [2] => Array
        (
            [project_name] => Invoice 78554
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-06-13 - 2016-06-19
                            [1] => 10
                        )

                )

        )

    [3] => Array
        (
            [project_name] => Small Move*
            [hours] => Array
                (
                    [0] => Array
                        (
                            [0] => 2016-06-27 - 2016-07-03
                            [1] => 3.5
                        )

                    [1] => Array
                        (
                            [0] => 2016-05-30 - 2016-06-05
                            [1] => 9.5
                        )

                )

        )

)

这是我的代码......

 $project_hours = array();
foreach($time_recs as $phours){
    $year = date('Y');
    $w =  date('W', strtotime($phours['work_date']));
    $time = strtotime("1 January $year", time());
    $day = date('w', $time);
    $time += ((7*$w)+1-$day)*24*3600;
    $return[0] = date('Y/m/d', $time);
    $time += 6*24*3600;
    $return[1] = date('Y/m/d', $time);
    $startend = $return[0]. ' - '. $return[1];

    $project_hours[$phours['project_id']]['project_name'] = $phours['project_name'];
    $project_hours[$phours['project_id']]['hours'][] = array(0 => $startend, 1 => (float)$phours['entry_hours']);
    $project_hours[$phours['project_id']]['project_hours'][] = (float)$phours['entry_hours'];

}

foreach($project_hours as $hours) {
    if (array_key_exists($hours['hours'][1], $project_hours[$hours['hours']][1])) {
        $project_hours[$hours['project_name']]['time'][] += $hours['hours'][1];
    }else{
        $project_hours[$hours['project_name']]['time'][] = $hours['hours'][1];
    }
}

0 个答案:

没有答案