0

I am trying to merge elements within an array if the StartingDateTime has a common value and create another array at TWHour and TEHour along with the respective values of merged arrays

Do note that there can be more arrays with the same StartingDateTime.

Here's an extract of the $items array:

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
        )
)

Result Array

    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 Answers1

0

I think it will help you.

Try this:

$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 is the final result.

Bilas Sarker
  • 459
  • 3
  • 10
  • Thanks for the reply. The answer you've provided does the job of creating an array at TWHour and TEHour. However, it does not fulfill the condition to only merge if it has the same StartingDateTime – Justin Liwag Jun 22 '16 at 06:11