如何"结合"阵列

时间:2015-05-08 16:21:16

标签: php db2

我正在查询DB2平面文件和常规表以获取信息。最新数据将来自非平面文件。我将两个结果放入两个独立的数组中。理想情况下,我认为加入表格是最好的,但我尝试过,我不知道你是否可以将平面文件加入非平面文件表。这是数组1。

Array
(
    [0] => Array
        (
            [pEmpNum] => 450
            [trCode] => 1
            [pRate] => 40.00
            [pWeeksWorked] => 2.00
            [pAppr] => J014 1       
        )

)

这是第二个

的一部分
1Array
(
[12] => Array
        (
            [empNum] => 160    
            [location] => 2         
            [payGroup] => 3
            [weeksWorked] => 02
            [defaultPay] => Y
            [unitRate] => 75.00
            [rate] => 18.67
            [appFlag] =>  
            [app1] => J014 1       
            [app2] => 
            [app3] => 
            [app4] => 
            [app5] => 
            [unit1] => .00
            [unit2] => .00
            [unit3] => .00
            [unit4] => .00
            [unit5] => .00
            [gross] => 1400.25
        )

    [13] => Array
        (
            [empNum] => 450        
            [location] => 2
            [payGroup] => 3
            [weeksWorked] => 00
            [defaultPay] =>  
            [unitRate] => 0.00
            [rate] => 24.63
            [appFlag] =>  
            [app1] => J014 1       
            [app2] => 
            [app3] => 
            [app4] => 
            [app5] => 
            [unit1] => .00
            [unit2] => .00
            [unit3] => .00
            [unit4] => .00
            [unit5] => .00
            [gross] => 0.00
        )

)

我想要做的是,如果第一个数组中存在empnum,我想要替换array1中的所有信息,并将其放入array2中以获得相同的empnum。所以在这个例子中,因为empnum 450存在于两个数组中,我想用singleRate替换pRate,用workWeate替换pWeeksWorked,用appApp用pAppr替换。关于如何做到这一点的任何帮助或更好的建议都会很棒。

1 个答案:

答案 0 :(得分:0)

这是我的建议。 $array1是第一个数组,$array2是第二个数组。循环使用它们,如果empnum匹配,则交换相关值。这是一个“纯粹的”PHP解决方案。

foreach($array1 as $key1 => $data1)
{
  foreach($array2 as $key2 => $data2)
  {
    if ($data1['pEmpNum'] == $data2['empNum'])
    {
      $data2['unitRate'] = $data1['pRate'];
      $data2['weeksWorked'] = $data1['pWeeksWorked'];
      $data2['app1'] = $data1['pAppr'];
    }
  }
}