2012-01-25 81 views
0

我花了很长时间研究如何在数组之间执行此操作,但我无法在PHP中获得算法我返回了这些结果。比较数组,并显示差异PHP

阵列#1:

Array DB_ITEMS 
(
    [1] => Array 
     (
      [item_code] => FO1321 
      [item_quantity] => 5 
      [item_sellprice] => 18.00 
      [found] => 0 
     ) 

    [2] => Array 
     (
      [item_code] => HE240 
      [item_quantity] => 1 
      [item_sellprice] => 22.40 
      [found] => 0 
     ) 

) 

阵列#2:

Array BUY_ITEMS 
(
    [1] => Array 
     (
      [item_code] => FO1321 
      [item_quantity] => 1 
      [item_sellprice] => 18.00 
      [taken] => 0 
     ) 

    [2] => Array 
     (
      [item_code] => EL55 
      [item_quantity] => 1 
      [item_sellprice] => 8.00 
      [taken] => 0 
     ) 

) 

我想这个结果,以阵列形式:

Array FINAL_RESULT 
(
    [1] => Array 
     (
      [item_code] => FO1321 
      [item_quantity] => -4 
      [item_sellprice] => 22.40 
      [taken] => 0 
     ) 

    [2] => Array 
     (
      [item_code] => HE240 
      [item_quantity] => -1 
      [item_sellprice] => 22.40 
      [taken] => 0 
     ) 

    [3] => Array 
     (
      [item_code] => EL55 
      [item_quantity] => +1 
      [item_sellprice] => 8.00 
      [taken] => 0 
     ) 

) 

我这样做比较正在修改的现有帐单。 我需要设置它们之间的差异,然后在数据库中进行更改。

+0

任何想法做到这一点?,我已经打破了我的头脑思考如何! :( –

+0

解释FO1321的逻辑:[item_sellprice] => 22.40。当然,如果不是拼写错误 – Cheery

回答

2

当你没有提供(仅适用于量)过多的逻辑......

$DB_ITEMS = array(
    array(
     'item_code' => 'FO1321', 
     'item_quantity' => 5, 
     'item_sellprice' => 18, 
     'found' => 0 
    ), 
    array(
     'item_code' => 'HE240', 
     'item_quantity' => 1, 
     'item_sellprice' => 22.4, 
     'found' => 0 
    ) 
    ); 

    $BUY_ITEMS = array(
    array(
     'item_code' => 'FO1321', 
     'item_quantity' => 1, 
     'item_sellprice' => 18, 
     'taken' => 0 
    ), 
    array(
     'item_code' => 'EL55', 
     'item_quantity' => 1, 
     'item_sellprice' => 8, 
     'taken' => 0 
    ) 
    ); 

    // fast fix 
    $db = array(); $buy = array(); 
    foreach($DB_ITEMS as $item) 
    $db[$item['item_code']] = $item; 
    foreach($BUY_ITEMS as $item) 
    $buy[$item['item_code']] = $item; 
    unset($DB_ITEMS, $BUY_ITEMS); 

    // now deal with arrays 
    foreach($db as $code=>$item) 
    if (array_key_exists($code, $buy)) 
     $buy[$code]['item_quantity'] -= $item['item_quantity']; 
    else 
    { 
     $buy[$code] = $item; 
     $buy[$code]['item_quantity'] =- $item['item_quantity']; 
     $buy[$code]['taken'] = $buy[$code]['found']; 
     unset($buy[$code]['found']); 
    } 

    // output result 
    var_dump($buy); 

结果:

array(3) { 
    ["FO1321"]=> 
    array(4) { 
    ["item_code"]=> 
    string(6) "FO1321" 
    ["item_quantity"]=> 
    int(-4) 
    ["item_sellprice"]=> 
    int(18) 
    ["taken"]=> 
    int(0) 
    } 
    ["EL55"]=> 
    array(4) { 
    ["item_code"]=> 
    string(4) "EL55" 
    ["item_quantity"]=> 
    int(1) 
    ["item_sellprice"]=> 
    int(8) 
    ["taken"]=> 
    int(0) 
    } 
    ["HE240"]=> 
    array(4) { 
    ["item_code"]=> 
    string(5) "HE240" 
    ["item_quantity"]=> 
    int(-1) 
    ["item_sellprice"]=> 
    float(22.4) 
    ["taken"]=> 
    int(0) 
    } 
} 
+0

你的答案解决了这个问题,非常感谢你 –

3

用于计算结果数组值的数学算法对我来说没有意义(我怀疑你可能在你的文章中输入了错误的数据)。

如果我是你,我会创建使用item_code作为数组键的关联数组,所以当你进行比较时,你可以在现场更新值。

Array DB_ITEMS 
(
    [FO1321] => Array 
     (
      [item_code] => FO1321 
      [item_quantity] => 5 
      [item_sellprice] => 18.00 
      [found] => 0 
     ) 

    [HE240] => Array 
     (
      [item_code] => HE240 
      [item_quantity] => 1 
      [item_sellprice] => 22.40 
      [found] => 0 
     ) 

) 

Array BUY_ITEMS 
(
    [FO1321] => Array 
     (
      [item_code] => FO1321 
      [item_quantity] => 1 
      [item_sellprice] => 18.00 
      [taken] => 0 
     ) 

    [EL55] => Array 
     (
      [item_code] => EL55 
      [item_quantity] => 1 
      [item_sellprice] => 8.00 
      [taken] => 0 
     ) 

) 

现在,你可以通过购买物品排列工序和更新数据库项目阵列(未经测试):

foreach ($buyItems as $itemCode => $buyItem) { 
    if (array_key_exists($itemCode, $dbItems)) { 
     $dbItems[$itemCode]['item_quantity'] =- $buyItem['item_quantity']; 
    } 
}