Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I would like to subtract stocks from $array1 and quantity from $array2,

$array1= ([product_id]=>4, [stocks]=>20)

$array2= ([product_id]=>4, [quantity]=>3)

So that would be:
$array1= ([0]=> 4, [1] => 20);
$array2= ([0]=> 4, [1] => 3);

And then the output should be:

$array1= ([0]=> 4, [1] => 17);

Need your help guys..

share|improve this question
1  
This can be done by looping through one array and comparing keys with the other, then doing the math as needed. What have you tried so far? Some code and where you are failing would be helpful. –  Crackertastic Oct 22 '13 at 19:01
    
$total = $array1['stocks'] - $array2['quantity'] –  Marc B Oct 22 '13 at 19:07
    
@Crackertastic I tried array_diff() but it only returns one element, I really new to this kind of array manipulation :( –  Juan D Jensen Oct 22 '13 at 19:08
    
@JuanDJensen I see. array_diff() isn't actually used for mathematical calculation between arrays. What it does is looks for elements that aren't common between multiple arrays and returns a new array of those elements. I think in your case a loop implementation will be best. Something that looks at array keys and then does the proper math. –  Crackertastic Oct 22 '13 at 19:12

2 Answers 2

up vote 1 down vote accepted

Your array structure looks slightly different with multiple records, the code works out like this in an ugly manner. I'm assuming you're talking about something like this:

$array1 = array(
    0=>array('product_id'=>4, 'stocks'=>20), 
    1=>array('product_id'=>5, 'stocks'=>60));
$array2 = array(
    0=>array('product_id'=>4, 'quantity'=>3)
    1=>array('product_id'=>5, 'quantity'=>30));

...It's a multi-dimensional array (typical for records pulled from a database).

foreach($array1 as $key=>$value){
    foreach($array2 as $key2=>$value2) {
        if($value['product_id']==$value2['product_id']){
            $value['stocks'] -= $value2['quantity'];
            //optimization to avoid searching this again.
            unset($array2[$key]);
        }
    }}
share|improve this answer

With what you have given the following will do what you are asking for:

if($array1['product_id'] == $array2['product_id']) {
    $array1['stocks'] -= $array2['quantity'];
}

If you need to loop through a bigger array then what I have given is only part of the larger puzzle.

share|improve this answer
    
What if there are more than one products ? any idea ? :) –  Juan D Jensen Oct 22 '13 at 19:34
    
Well, I would guess you are dealing with two multidimensional arrays in that case. It it were me I would loop using a for loop to go through the $array1 array to get my product_id and stocks. Within that loop I would use another for loop to go through $array2 and look for a matching product_id and then subtract any quantity values from stocks in $array1. Optionally, I may nullify values in $array2 that I have processed so I can reduce the amount of work the inner for loop must do. –  Crackertastic Oct 22 '13 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.