如何比较两个数组并向表中添加唯一记录

时间:2014-10-30 17:29:09

标签: php mysql arrays

我正在尝试比较并添加从表B到表A的唯一记录,并跳过重复记录(如果存在)并在下一条记录上执行搜索,如果找不到匹配项,则添加它。 使用[SalesDescription]字段

进行比较

我尝试使用array_unique和array_differ,array_intersect但这些函数都不能摆脱重复/不能为我工作。

这是代码添加数据

  $records=array(
      'SalesDescription'         => $row['SalesDescription'],
      'SO Qty' => $row['Quantity'],
      'WO Qty'          => $row['Reference'],
      'SO Date'          => $row['TransactionDate'],
      'Color'         => $row['DistNumber'], 
      'SO#'         => $row['Reference'], 
      'Promised Date'         => $row['GoodThruDate'], 



     );

    $tv->addRecord($tableId, $records);//calls addRecord() function

表A:从表中我想要将数据添加到

中的记录
Array ( 
      [id] => 3054535376 
      [table_id] => 50240233993 
      [created] => 2014-10-30T09:52:15-0600 
      [updated] => 2014-10-30T09:52:15-0600 
      [created_by] => 3045444045538 
      [fields] => Array ( 
                        [Work Order] => 87565 
                        [SalesDescription] => 18X5X12 1/8 SOLID FORKLIFT TIRE PRESS-ONS 
                        [SO#] => 14061 
                        [SO Date] => 2013-10-16T00:00:00-0600 
                        [Promised Date] => 2014-10-30T09:52:15-0600 
                        [SO Qty] => 1.0000000000000000000 
                        [WO Qty] => 14061 
                        [Color] => 2 
                        [s] => 2 
                        [SHIP BY] => 2014-10-29 
                        [CALC PROMISED DATE] => 2014-10-30 
                        ) 
   ) 

表B:我希望从

获取数据的表中的记录
 Array ( 
       [0] => CAS001 
       [CustomerId] => CAS001 
       [1] => CASH CUSTOMER 
       [Customer_Bill_Name] => CASH CUSTOMER 
       [2] => Will Call 
       [WhichShipVia] => Will Call 
       [3] => [INV_POSOOrderNumber] => [4] => 2013-07-24 
       [ShipByDate] => 2013-07-24 
       [5] => [GoodThruDate] => [6] => [CustomerSONo] => [7] => 13867 
       [Reference] => 13867 [8] => 2013-07-24 
       [TransactionDate] => 2013-07-24 
       [9] => 1 
       [DistNumber] => 1 
       [10] => 2.0000000000000000000 
       [Quantity] => 2.0000000000000000000 
       [11] => 121130581 
       [ItemId] => 121130581 
       [12] => 18X5X12 1/8 SOLID FORKLIFT TIRE PRESS-ONS 
       [SalesDescription] => 18X5X12 1/8 SOLID FORKLIFT TIRE PRESS-ONS 
       [13] => [PartNumber] => [14] => 18X5X12 1/8 SOLID FORKLIFTTIRE 
       [ItemDescription] => 18X5X12 1/8 SOLID FORKLIFTTIRE
       ) 

1 个答案:

答案 0 :(得分:0)

这有点麻烦,但您可以先创建一个带键的数组(即SalesDescription值)。 Double SalesDescription将相互覆盖,然后将其转换回来:

<?php

$tableB = array(....)

$temp = array();
foreach($tableB as $value) {
  $temp[$value['SalesDescription']] = $value;
}
$tableB = array();
foreach($temp as $value) {
  $tableB[] = $value;
}

// Now $tableB has only unique SalesDescription's!