需要帮助过滤来自两个多维数组的重复项,然后创建唯一的数组

时间:2014-11-02 04:13:13

标签: php multidimensional-array

我正在尝试过滤并从数组中删除重复值并向表中添加新值。我尝试使用以下代码。我已经工作了好几个小时请帮忙!

$tmpArray = array();
while($row = mysqli_fetch_array($result)) {
     $duplicate = false;
     foreach($recordd as $data2) {
     if($row['SalesDescription'] === $data2['fields']['WO Notes'] ) $duplicate = true;
     }

    if($duplicate === false)
       $tableId = 50240233993;
       $tmpArray = $row;

    $records=array(
      'SO Qty' => 'Test',
      'WO Qty'          => $tmpArray['Reference'],
      'SO Date'          => $tmpArray['TransactionDate'],
      'Color'         => $tmpArray['DistNumber'], 
      'SO#'         => $tmpArray['Reference'], 
      'Promised Date'         => $tmpArray['ItemId'], 
      'WO Notes'         => $tmpArray['SalesDescription'], 

   );


  $tv->addRecord($tableId, $records);// call to addRecord() function which adds data to Cloud table
 }

这是旧记录

Array ( 
  [id] => 56554535376 
  [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 
                    ) 

以下是新记录

 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)

正如我在评论中提到的那样,您错过了{ }块。完全解释:

if($duplicate === false) ///<-- no brace
   $tableId = 50240233993;
   $tmpArray = $row;  //<-- this will execute no matter the value of $duplicate

$records=array(
  'SO Qty' => 'Test',
  'WO Qty'          => $tmpArray['Reference'],
  'SO Date'          => $tmpArray['TransactionDate'],
  'Color'         => $tmpArray['DistNumber'], 
  'SO#'         => $tmpArray['Reference'], 
  'Promised Date'         => $tmpArray['ItemId'], 
  'WO Notes'         => $tmpArray['SalesDescription'], 

 );


 $tv->addRecord($tableId, $records);// call t...

基本上你所拥有的是这个

if($duplicate === false){
   $tableId = 50240233993;
}
$tmpArray = $row;  //<-- this will execute no matter the value of $duplicate
....

你只是告诉php设置id如果它不是重复然后其余的执行因为它没有包裹在大括号中,试试这个

if($duplicate === false){
        $tableId = 50240233993;
        $tmpArray = $row;

     $records=array(
       'SO Qty' => 'Test',
       'WO Qty'          => $tmpArray['Reference'],
       'SO Date'          => $tmpArray['TransactionDate'],
       'Color'         => $tmpArray['DistNumber'], 
       'SO#'         => $tmpArray['Reference'], 
       'Promised Date'         => $tmpArray['ItemId'], 
       'WO Notes'         => $tmpArray['SalesDescription'], 

     );


      $tv->addRecord($tableId, $records);// call t
}