2016-06-13 9 views
-1

私は約250人の従業員の配列を持っています。私は彼らの配列の1つまたは2つの要素が異なるほとんどの従業員の重複を持っています。同じ "empNum"を持つ配列を検出して組み合わせるにはどうしたらいいですか?以下の例は、私が133個の要素を組み合わせた例です。 1は、上記から配列および他の配列に明確なempNumsを得るために:私は試してみました何どのように自分自身に配列を結合するのですか

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 
    ) 

) 

は、2つのクエリです。私は2つのforeachをしましたが、要素をそれ自身と比較し、要素が変更されると次の要素に移動する方法はわかりません。私はarray_merge()とarray_merge_recursive()も試みました。

+0

DId自分で何かを試してみましたか?それが何だった?ここでそれを共有してください。 –

+1

[値に基づいてユニークな多次元配列(配列キーではない)]の可能な複製(http://stackoverflow.com/questions/6057151/multidimensional-array-unique-based-onvalue-not-array-key) – Midas

+0

Iそのコードを試しました。私は何かを取り除きたいとは思わない。私は重複を保ちたい。 – moe

答えて

0

コード

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

$ finalOutputの下に試してみてください、私はphp.netにこのソリューションを発見したユニークなEMPNUMで

+0

これは私に上記の元の配列と同じことを与えます。 – moe

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 
) 

項目の値です。私はこれがあなたが探しているものだと思います。

関連する問題