0

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.

2
  • Any idea to do this?, I have broken my head thinking about how! :( Commented Jan 25, 2012 at 3:48
  • Explain the logic for FO1321: [item_sellprice] => 22.40. If it is not a typo, of course. Commented Jan 25, 2012 at 4:04

3 Answers 3

3

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'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.