比较数组,并显示PHP的差异


Comparing Array, and show the differences 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
        )
)

我这样做是为了比较正在修改的现有法案。我需要设置它们之间的差异,然后在DB中进行更改。

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

如果我是你,我会使用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
        )
)

现在您可以遍历buy-items数组并更新db items数组(未测试):

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

由于您没有提供太多逻辑(仅针对数量)。。。

  $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)
  }
}

看看数组交集uassoc,因为它似乎正是您想要的