2016-03-21 99 views
0

我試圖組合兩個數組並在數組中減去一個值,如果兩個鍵彼此匹配的話。如果兩個值匹配,組合/修改PHP數組

這是我迄今爲止編寫的代碼。

$dat = array(); 
for ($i = 0; $i <= (count($cashdrawer_sales) - 1); $i++) { 
$obj = $cashdrawer_sales[$i]; 
foreach ($cashdrawer_refund as $k=>$ref) { 
    if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) { 
     $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total']; 
     $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total); 
     array_push($dat,$arr); 
    } else { 
     $cash_drawer_total = $obj['cash_drawer_sale_total']; 
     // echo $cash_drawer_total."<br>"; 
     $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total); 
     array_push($dat,$arr); 
    } 
} 
} 
print_r($dat); 

下面是我想要操作的$ cashdrawer_sales和$ cashdrawer_refund數組的示例。

cashdrawer_sales

Array 
(
[0] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21112 
     [cash_drawer_sale_total] => 64.00 
    ) 

[1] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21117 
     [cash_drawer_sale_total] => 15.00 
    ) 

[2] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_sale_total] => 50.00 
    ) 

[3] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21112 
     [cash_drawer_sale_total] => 193.00 
    ) 

[4] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21113 
     [cash_drawer_sale_total] => 30.00 
    ) 

[5] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21117 
     [cash_drawer_sale_total] => 10.00 
    ) 

[6] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_sale_total] => 80.00 
    ) 

[7] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => 21112 
     [cash_drawer_sale_total] => 50.00 
    ) 

[8] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_sale_total] => 125.00 
    ) 

) 

cashdrawer_refund

Array 
(
[0] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21112 
     [cash_drawer_refund_total] => 103.00 
    ) 

[1] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_refund_total] => 25.37 
    ) 
) 

我覺得我真的很接近的解決方案,但我不能得到正確

當我運行這段代碼我m爲每個原始價值獲得兩個金額。所以(從我所知道的情況來看)我在多次通過時滿足了我的條件。

Array 
(
[0] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 64.00 
    ) 

[1] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 64.00 
    ) 

[2] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 15.00 
    ) 

[3] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 15.00 
    ) 

[4] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 50.00 
    ) 

[5] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 50.00 
    ) 

[6] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 90 
    ) 

[7] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 193.00 
    ) 

[8] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21113 
     [cash_drawer_total] => 30.00 
    ) 

[9] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21113 
     [cash_drawer_total] => 30.00 
    ) 

[10] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 10.00 
    ) 

[11] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 10.00 
    ) 

[12] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 80.00 
    ) 

[13] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 80.00 
    ) 

[14] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 50.00 
    ) 

[15] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 50.00 
    ) 

[16] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 125.00 
    ) 

[17] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 99.63 
    ) 

) 

我假設發生這種情況是因爲我通過兩次銷售迭代來分析退款。但是,我不知道該怎麼做。

這是我的終極目標是:

Array 
(
[0] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 64.00 
    ) 

[1] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 15.00 
    ) 


[2] => Array 
    (
     [department_id] => 80000 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 50.00 
    ) 


[3] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 90 
    ) 


[4] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21113 
     [cash_drawer_total] => 30.00 
    ) 


[5] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => 21117 
     [cash_drawer_total] => 10.00 
    ) 

[6] => Array 
    (
     [department_id] => 50502 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 80.00 
    ) 


[7] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => 21112 
     [cash_drawer_total] => 50.00 
    ) 

[8] => Array 
    (
     [department_id] => No Department 
     [cash_drawer] => No Cash Drawer 
     [cash_drawer_total] => 99.63 
    ) 
) 

有什麼建議?

+0

是否要爲每個cash_drawer單獨銷售和退款? –

+0

我想從相應的銷售總額中扣除退款總額,然後創建新總計的新數組。 –

+0

好的,但你想爲每個cash_drawer做這項工作嗎?由於您的銷售陣列有7個元素,您的退款陣列有2個元素。 –

回答

1

使用的department_idcash_drawer組合來創建結果鍵:使用array_values()

$result = array(); 

foreach($cashdrawer_sales as $row) 
{ 
    $key = $row['department_id'].'|'.$row['cash_drawer'];      # <------ 
    if(!isset($result[$key])) 
    { 
     $result[$key] = $row; 
     unset($result[$key]['cash_drawer_sale_total']); 
     $result[$key]['cash_drawer_total'] = 0; 
    } 
    $result[$key]['cash_drawer_total'] += $row['cash_drawer_sale_total']; 
} 

foreach($cashdrawer_refund as $row) 
{ 
    $key = $row['department_id'].'|'.$row['cash_drawer'];      # <------ 
    if(!isset($result[$key])) 
    { 
     $result[$key] = $row; 
     unset($result[$key]['cash_drawer_refund_total']); 
     $result[$key]['cash_drawer_total'] = 0; 
    } 
    $result[$key]['cash_drawer_total'] -= $row['cash_drawer_refund_total']; 
} 

然後刪除鍵:

$result = array_values($result); 

這是$result現在:

Array 
(
    [0] => Array 
     (
      [department_id] => 80000 
      [cash_drawer] => 21112 
      [cash_drawer_total] => 64 
     ) 

    [1] => Array 
     (
      [department_id] => 80000 
      [cash_drawer] => 21117 
      [cash_drawer_total] => 15 
     ) 

    [2] => Array 
     (
      [department_id] => 80000 
      [cash_drawer] => No Cash Drawer 
      [cash_drawer_total] => 50 
     ) 

    [3] => Array 
     (
      [department_id] => 50502 
      [cash_drawer] => 21112 
      [cash_drawer_total] => 90 
     ) 

    [4] => Array 
     (
      [department_id] => 50502 
      [cash_drawer] => 21113 
      [cash_drawer_total] => 30 
     ) 

    [5] => Array 
     (
      [department_id] => 50502 
      [cash_drawer] => 21117 
      [cash_drawer_total] => 10 
     ) 

    [6] => Array 
     (
      [department_id] => 50502 
      [cash_drawer] => No Cash Drawer 
      [cash_drawer_total] => 80 
     ) 

    [7] => Array 
     (
      [department_id] => No Department 
      [cash_drawer] => 21112 
      [cash_drawer_total] => 50 
     ) 

    [8] => Array 
     (
      [department_id] => No Department 
      [cash_drawer] => No Cash Drawer 
      [cash_drawer_total] => 99.63 
     ) 

) 
+0

謝謝你。 這是一個我自己不會想到的解決方案,它比我嘗試的更合乎邏輯。 –

0

我已對您的代碼進行了一些更改我想我有你在找什麼......

$dat = array(); 
for ($i = 0; $i < (count($cashdrawer_sales)); $i++) { 
    $obj = $cashdrawer_sales[$i]; 
    foreach ($cashdrawer_refund as $k=>$ref) { 
     if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) { 
      $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total']; 
      $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total); 
      $dat[] = $arr; 
     } else { 
      if (!in_array($obj, $dat)) { // <-- check the array for the item, if it's not there then add it 
       $dat[] = $obj; 
      } 
     } 
    } 
} 

foreach($dat as $test){ 
    print_r($test); 
    echo "<br>"; 
} 

基本上,在將當前項目添加回數組之前,我檢查它是否已經存在。

0
$dat = array(); 
for ($i = 0; $i <= (count($cashdrawer_sales) - 1); $i++) { 


    $obj = $cashdrawer_sales[$i]; 
    $match=false; // assume initially that this $obj does not match any $ref till now 
    foreach ($cashdrawer_refund as $k=>$ref) { 
    //check if this ref matches if yes then break//you are for all obj each time it mathches as well as not matches an obj 
     if ($obj['cash_drawer']==$ref['cash_drawer'] && $ref['department_id']==$obj['department_id']) { 
       $match=true; 
       break;  
     } 
    } 
    // this accounts for one obj only once in your output 
    if($match){ 
     $cash_drawer_total = $obj['cash_drawer_sale_total'] - $ref['cash_drawer_refund_total']; 
     $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total); 
     array_push($dat,$arr); 
    }else{ 
     $cash_drawer_total = $obj['cash_drawer_sale_total']; 
      // echo $cash_drawer_total."<br>"; 
     $arr = array('department_id'=>$obj['department_id'], 'cash_drawer' => $obj['cash_drawer'], 'cash_drawer_total' => $cash_drawer_total); 
     array_push($dat,$arr); 
    } 
} 
print_r($dat); 
+0

我還要補充說這不是執行此操作的最快方法。您可以對數組1和數組2進行排序,然後使用更好的檢查在O(n logn)而不是O(n^2)中完成此操作, – techblu3

0

要解決這樣的問題,最好的方法是在你的頭腦或紙上解決它然後實現它。

據我所知,你想修改銷售總額爲有退款的人。那麼你需要檢查你所有的銷售:

foreach ($cashdrawer_sales as $sale_key => $sale) { 
} 

然後,我們要檢查這個部門的現金抽屜是否有退款。我們檢查所有退款:

foreach ($cashdrawer_sales as $sale_key => $sale) { 
    foreach ($cashdrawer_refund as $refund_key => $refund) { 
     if ($sale['cash_drawer'] === $refund['cash_drawer'] && 
       $sale['department_id'] === $refund['department_id']) { 
      // and if there was refund then we need to subtract it: 
      $cashdrawer_sales[$sale_key]['new_total'] = $sale['cash_drawer_sale_total'] - 
        $refund['cash_drawer_refund_total']; 
     } 
    } 
} 

這應該做的工作。不是說要在同一個陣列中提交更改,我使用了密鑰($cashdrawer_sales[$sale_key]['new_total'])

希望有所幫助。