John Nuñez
John Nuñez

Reputation: 1830

Comparing Array, and show the differences PHP

I have taken a long time researching how to perform this operation between arrays but I can not get an algorithm in PHP I return these results.

Array #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
        )

)

Array #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
        )

)

I want this Result, in an Array Format:

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
        )

)

I'm doing this to compare an existing bill that is being modified. And I need to set the differences between them and then make changes in DB.

Upvotes: 0

Views: 120

Answers (3)

Cheery
Cheery

Reputation: 16214

As you did not provide too much logic (only for quantities)...

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

Result:

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

Upvotes: 2

Bogdan
Bogdan

Reputation: 865

have a look at array intersect uassoc as it seems to be just what you want

Upvotes: 1

Mike Purcell
Mike Purcell

Reputation: 19979

The math used to calculate the values of the resulting array don't make sense to me (I suspect you may have entered the data wrong in your post).

If I were you I would create associative arrays using the item_code as the array key, so when you do comparisons, you can update the values on the spot.

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
        )

)

Now you can step through the buy items array and update the db items array (untested):

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

Upvotes: 3

Related Questions