使用条件合并数组中的元素

时间:2016-06-21 07:08:01

标签: php arrays multidimensional-array

如果StartingDateTime具有公共值并且在TWHour和TEHour创建另一个数组以及合并数组的相应值,我正在尝试合并数组中的元素

请注意,可以有更多具有相同StartingDateTime的数组。

以下是$items数组的摘录:

Array
(
    [0] => Array
        (
            [StartingDateTime] => 2016-06-25
            [TWHour] => 13:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => can rest
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )

    [1] => Array
        (
            [StartingDateTime] => 2016-06-26
            [TWHour] => 19:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => any time
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )

    [2] => Array
        (
            [StartingDateTime] => 2016-06-27
            [TWHour] => 13:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => I 
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )

    [3] => Array
        (
            [StartingDateTime] => 2016-06-28
            [TWHour] => 03:00:00
            [TEHour] => 05:00:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => want
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )

    [4] => Array
        (
            [StartingDateTime] => 2016-06-28
            [TWHour] => 13:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => want
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )
    [5] => Array
        (
            [StartingDateTime] => 2016-06-29
            [TWHour] => 13:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => to rest
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )

    [6] => Array
        (
            [StartingDateTime] => 2016-06-30
            [TWHour] => 12:30:00
            [TEHour] => 23:30:00
            [NoOfHoursWorked] => 10
            [NoOfHoursRest] => 14
            [Comments] => hehe
            [NoOfHoursRestAny24HR] => 14
            [NoOfHoursRestAny7Day] => 98
        )
)

结果数组

    Array
        (
        [0] => Array
            (
                [StartingDateTime] => 2016-06-25
                [TWHour] => 13:30:00
                [TEHour] => 23:30:00
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => can rest
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )

        [1] => Array
            (
                [StartingDateTime] => 2016-06-26
                [TWHour] => 19:30:00
                [TEHour] => 23:30:00
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => any time
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )

        [2] => Array
            (
                [StartingDateTime] => 2016-06-27
                [TWHour] => 13:30:00
                [TEHour] => 23:30:00
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => I 
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )

        [3] => Array
            (
                [StartingDateTime] => 2016-06-28
                [TWHour] => Array
                   (
                      [0] => 03:00:00
                      [1] => 13:30:00
                   )
                [TEHour] => Array
                   (
                      [0] => 05:00:00
                      [1] => 23:30:00
                   )
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => want
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )
        [4] => Array
            (
                [StartingDateTime] => 2016-06-29
                [TWHour] => 13:30:00
                [TEHour] => 23:30:00
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => to rest
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )

        [5] => Array
            (
                [StartingDateTime] => 2016-06-30
                [TWHour] => 12:30:00
                [TEHour] => 23:30:00
                [NoOfHoursWorked] => 10
                [NoOfHoursRest] => 14
                [Comments] => hehe
                [NoOfHoursRestAny24HR] => 14
                [NoOfHoursRestAny7Day] => 98
            )

    )

1 个答案:

答案 0 :(得分:0)

我认为它会对你有所帮助。

试试这个:

$arr = array(
    array(
            'StartingDateTime' => "2016-06-28",
            'TWHour' => "03:00:00",
            'TEHour' => "05:00:00",
            'NoOfHoursWorked' => 10,
            'NoOfHoursRest' => 14,
            'Comments' => "want",
            'NoOfHoursRestAny24HR' => 14,
            'NoOfHoursRestAny7Day' => 98
        ), 
    Array
        (
            'StartingDateTime' => "2016-06-28",
            'TWHour' => "13:30:00",
            'TEHour' => "23:30:00",
            'NoOfHoursWorked' => 10,
            'NoOfHoursRest' => 14,
            'Comments' => "want",
            'NoOfHoursRestAny24HR' => 14,
            'NoOfHoursRestAny7Day' => 98
        )
    );

$results = array();
array_map(function($a, $b) use (&$results) {    
    $tmp = array($a['TWHour'], $b['TWHour']);
    $a['TWHour'] = $b['TWHour'] = $tmp; 

    $tmp = array($a['TEHour'], $b['TEHour']);
    $a['TEHour'] = $b['TEHour'] = $tmp; 

    $results += array_merge($a,$b);
}, array($arr[0]),array($arr[1]));

$ results 是最终结果。