我如何将数组与自身结合起来

时间:2016-06-13 13:58:42

标签: php arrays

我有大约250名员工。我有大多数员工的副本,其中一个或两个元素的元素是不同的。如何检测和组合具有相同" empNum"的数组。下面的例子是一个例子,当合并时我会有133个元素。

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
    )

)

我所尝试的是两个查询:一个用于将不同的empNums放入一个数组中,另一个用于从上面获取数组。然后我做了两个foreach,但我不知道如何将元素与自身进行比较,然后在元素发生变化时转到下一个元素。我还尝试过array_merge()和array_merge_recursive()。

2 个答案:

答案 0 :(得分:0)

尝试以下代码

[('Red', (0.4745098039215686, 0.7372549019607844, 0.23137254901960794)), ....

$ finalOutput是具有唯一empNum

的项目的值

答案 1 :(得分: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
) 

我在php.net找到了这个解决方案。我认为这就是你要找的东西。