How should I subtract arrays

Asked

Viewed 1,483 times

1

array_intersect_ukey($result1, $result2, 'key_compare_func2') // primeiro array
array_intersect_ukey($result2, $result1, 'key_compare_func2') // segundo array

Upshot:

Array
(
    [bife] => 3
)
Array
(
    [bife] => 2
)

Desired:

Array
(
    [bife] => 1
)
  • What do you mean? What do you intend to do?

  • the additional of a product... it checks what the customer adds with the standard product

  • The second array has only keys from the first array, or it can contain a key without reference?

2 answers

4


Follows a function that "merges" arrays, subtracting items with equal keys in both:

function subtrai_array( $arr1, $arr2 ) {
   foreach ( $arr2 as $chave => $valor ) {
       if( array_key_exists( $chave, $arr1 ) ) {
           $arr1[$chave] = $arr1[$chave] - $valor;
      } else {
         $arr1[$chave] = -$valor;
      }
   }
   return $arr1;
}

See working on IDEONE.

0

Based on the @Bacco example, I will propose another way using array_merge_recursive. Basically it will combine the 2 arrays and generate an array of 2 keys when there is combination, then a loop with subtraction.

The example considers 2 arrays (stock and sale), where items of sale exist expressly in stock - sells only that is in stock.

$result = array_merge_recursive($a1, $a2);

foreach ($result as $key => $value )
{
    if( is_array( $value ) )
    {
        $array[ $key ] = ($value[0] - $value[1]);
    }
    else
    {
        $array[ $key ] = $value;
    }
}

Example in ideone, output:

+------------+-----------+-----------+-----------+-----------+
| estoque    | produto.1 | produto.2 | produto.3 | produto.4 |
| quantidade | 2         | 5         | 3         | 9         |
+------------+-----------+-----------+-----------+-----------+
| venda      | produto.1 |           | produto.3 |           |
| quantidade | 1         |           | 2         |           |
+------------+-----------+-----------+-----------+-----------+
| resultado  | produto.1 | produto.2 | produto.3 | produto.4 |
| quantidade | 1         | 5         | 1         | 9         |
+------------+-----------+-----------+-----------+-----------+
  • I don’t know what else happened I don’t work

  • @Willian No ideone is working, which variables you used?

  • $fetchForm, $items['cor_id'] stock sale

  • The second array contains the same keys as the first, or contains some key that is not in the first array?

  • ([1] => 3 [6] => -2 ) by the Stock code reduced to this... now and the following the customer can change all snack settings by ex steak 3x standard amount of the recipe he can withdraw 1x and can add 1x 2x 3x lettuce in case he adds 2x so the system has to do a sub-stitution 1 steak for 1 lettuce and an additional 1 lettuce... then the system has to have additional/withdrawal/replacement... in my view it will always happen or replace and adicona or subistitui and remove /// or just subistitui

  • contains pd sale it and dynamic 100%

  • here’s a demo http://jsfiddle.net/qtx8jrLy/ of what the customer’s part would be like to do... he can remove and add the amount he wants... the ones already marked and the standard product recipe

  • That question is already solved?

  • yes I need this new

  • So I recommend opening a new more detailed question.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.