-1

I have an array of around 250 employees. I have duplicates of most employees where one or two elements of their array is different. How do I detect and combine the arrays that have the same "empNum". The one below is an example, when combined I would have 133 elements.

Array
(
[0] => Array
    (
        [empNum] => 17                                   
        [wksWrkd] => 2
        [trcode] => 1
        [appn] => 51000550000              
        [totalHours] => 20.00
        [rate] => 12.00
        [net] => 240.00
        [overHours] => .00
        [holidayHours] => .00
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 240.00
    )

[1] => Array
    (
        [empNum] => 17                                   
        [wksWrkd] => 2
        [trcode] => 1
        [appn] => 20000560000              
        [totalHours] => 34.00
        [rate] => 12.00
        [net] => 408.00
        [overHours] => .00
        [holidayHours] => .00
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 408.00
    )

[2] => Array
    (
        [empNum] => 17                                  
        [wksWrkd] => 2
        [trcode] => 1
        [appn] => 51000550005              
        [totalHours] => 54.00
        [rate] => 12.00
        [net] => 648.00
        [overHours] => .00
        [holidayHours] => .00
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 648.00
    )

[3] => Array
    (
        [empNum] => 17                                  
        [wksWrkd] => 2
        [trcode] => 1
        [appn] => 20000560005              
        [totalHours] => 13.00
        [rate] => 12.00
        [net] => 156.00
        [overHours] => .00
        [holidayHours] => .00
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 156.00
    )

[4] => Array
    (
        [empNum] => 17                                   
        [wksWrkd] => 2
        [trcode] => 4
        [appn] => 51000550000              
        [totalHours] => .00
        [rate] => .00
        [net] => .00
        [overHours] => 4.00
        [holidayHours] => 72.00
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 72.00
    )

[5] => Array
    (
        [empNum] => 17                                 
        [wksWrkd] => 2
        [trcode] => 4
        [appn] => 51000550005              
        [totalHours] => .00
        [rate] => .00
        [net] => .00
        [overHours] => .25
        [holidayHours] => 4.50
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 4.50
    )

[6] => Array
    (
        [empNum] => 17                                  
        [wksWrkd] => 2
        [trcode] => 4
        [appn] => 20000560005              
        [totalHours] => .00
        [rate] => .00
        [net] => .00
        [overHours] => .25
        [holidayHours] => 4.50
        [overtimePay] => .00
        [otherPay] => .00
        [holidayPay] => .00
        [ctu] => .00
        [cta] => .00
        [ptu] => .00
        [pta] => .00
        [sickHours] => .0000
        [vacaHours] => .0000
        [gross] => 4.50
    )

)

What I have tried is two queries: one to get distinct empNums into an array and the other the array from above. I then did two foreach's but I don't know how to compare an element to itself and then go to the next element once that elements changes. I've also tried array_merge() and array_merge_recursive().

moe
  • 725
  • 2
  • 8
  • 19
  • DId you try something on your own first? What was it? Share it here. – Pavel Petrov Jun 13 '16 at 14:02
  • 1
    Possible duplicate of [Multidimensional array unique based on value (not array key)](http://stackoverflow.com/questions/6057151/multidimensional-array-unique-based-on-value-not-array-key) – Midas Jun 13 '16 at 14:03
  • I tried that code. I don't want to remove anything. I want to keep duplicates. – moe Jun 13 '16 at 14:13
  • How do you want manage duplicates? I mean, in the first two rows appn is different. Do you want to maintain both? – CiroRa Jun 13 '16 at 14:47
  • How do you want manage duplicates? I mean, in the first two rows appn is different. Do you want to maintain both? Furthermore, do those values come from a sql query? – CiroRa Jun 13 '16 at 14:53
  • Yes and yes. This is the problem. The very first thing I tried was this. $i++; $test["app$i"]=$row2[0]; $test["hours$i"]=$row2[1]; $test["rate$i"]=$row2[2]; – moe Jun 13 '16 at 14:54

2 Answers2

0

Try below code

 $processedArray = array();
    $finalOutput = array();
    foreach($elements as $elem){
        if(!in_array($elem["empNum"],$processedArray)){
            $processedArray[] = $elem["empNum"];
            finalOutput[] = $elem;
          }  
    }

$finalOutput is the value of items with unique empNum

abhinsit
  • 3,214
  • 4
  • 21
  • 26
0
If two keys are the same, the second one prevails. 

Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => 2
    [b] => 3
)

But if you need to keep all values, you can use the function below:

<?php
function array_combine_($keys, $values)
{
    $result = array();
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
    return    $result;
}

print_r(array_combine_(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
    [a] => Array
        (
            [0] => 1
            [1] => 2
        )

    [b] => 3
) 

I have found this solution on php.net. I think this is what you're looking for.

CiroRa
  • 492
  • 3
  • 13