Conditional subtraction of arrays

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
xkevin
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 22, 2017 4:42 pm

Wed Nov 22, 2017 4:59 pm

Hi everyone I am having a hard time on how to subtract the two arrays with the same value.

I have two arrays which hold the data of my products. Sample below.
$transfer_in array

Code: Select all

 Array
    (
    [0] => Array
        (
            [product_id] => 2
            [product_qty] => 32
            [product_pcs] => 0
        )
    [1] => Array
        (
            [product_id] => 3
            [product_qty] => 353
            [product_pcs] => 2
        )
    [2] => Array
        (
            [product_id] => 5
            [product_qty] => 11
            [product_pcs] => 1
        )
)
$transfer_out array

Code: Select all

Array
(
    [0] => Array
        (
            [product_id] => 5
            [product_qty] => 1
            [product_pcs] => 1
        )
)
Now I have to subtract the two arrays with specific product_id the product. If product_id from both arrays are the same/ exist then subtract it. $transfer_in['product_qty']-$transfer_out['product_qty'] and $transfer_in['product_pcs']-$transfer_out['product_pcs']
How can I subtract each product_qty and product_pcs with same product_id on both arrays and put them as one array?
This is the expected ouput:
$output = ?

Code: Select all

Array
    (
    [0] => Array
        (
            [product_id] => 2
            [product_qty] => 32
            [product_pcs] => 0
        )
    [1] => Array
        (
            [product_id] => 3
            [product_qty] => 353
            [product_pcs] => 2
        )
    [2] => Array
        (
            [product_id] => 5
            [product_qty] => 10
            [product_pcs] => 0
        )
)
The array data comes from the database, I am using SELECT SUM to get the total qty and pcs of every products both from products in and products out.

User avatar
benanamen
New php-forum User
New php-forum User
Posts: 23
Joined: Mon Oct 16, 2017 1:06 pm

Wed Nov 22, 2017 6:17 pm

Since the data come from a Database we need to see your DB. You are asking for an answer to your attempted solution which is the incorrect approach.

The database is more than capable of providing the results you want without code gymnastics.
The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

xkevin
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Nov 22, 2017 4:42 pm

Thu Nov 23, 2017 7:53 pm

Here is my table structure:
log_id | log_prod_id | log_prod_qty | log_prod_pcs_qty | log_transaction_type | log_transaction_incharge | log_transaction_details | log_date | log_admin |
And here is my whole code. Getting all the sum of every item.

Code: Select all

### in ###
    $sql = "SELECT *, SUM(log_prod_qty) as total_in_qty, SUM(log_prod_pcs_qty) as total_in_pcs_qty FROM transaction_logs INNER JOIN products ON(log_prod_id = prod_id) WHERE log_transaction_type = 'transfer-in' GROUP BY log_prod_id;";
    $products_in= db::select($sql);

    foreach ($products_in as $product_in){
        $total_qty = $product_in['total_in_qty'];
        $total_remaining_pcs = $product_in['total_in_pcs_qty'];
        $equivalent_qty = $product_in['prod_equivalent_to_qty_measure']; 

        $unit_measure_in_qty = $product_in['log_prod_unit']; 
        $unit_measure_in_pcs = $product_in['log_prod_pcs_unit']; 

        if($total_remaining_pcs > $equivalent_qty){
            $total_qty += floor($total_remaining_pcs / $equivalent_qty);  // add truncated integer to qty
            $total_remaining_pcs = $total_remaining_pcs % $equivalent_qty;  // store remainder after division
        }


        $transfer_in_products[] = array(
            "product_id" => $product_in['prod_id'],
            "product_name" => $product_in['prod_name'],
            "product_qty" => $total_qty,
            "product_pcs" => $total_remaining_pcs
        );

    }


### out ###
    $sql = "SELECT *, SUM(log_prod_qty) as total_in_qty, SUM(log_prod_pcs_qty) as total_in_pcs_qty FROM transaction_logs INNER JOIN products ON(log_prod_id = prod_id) WHERE log_transaction_type = 'transfer-out' GROUP BY log_prod_id;";
    $products_out= db::select($sql);

    $transfer_out_products = array();
    foreach ($products_out as $product_out){

        $total_qty = $product_out['total_in_qty'];
        $total_remaining_pcs = $product_out['total_in_pcs_qty'];
        $equivalent_qty = $product_out['prod_equivalent_to_qty_measure']; 

        $unit_measure_in_qty = $product_out['log_prod_unit']; 
        $unit_measure_in_pcs = $product_out['log_prod_pcs_unit']; 

        if($total_remaining_pcs > $equivalent_qty){
            $total_qty += floor($total_remaining_pcs / $equivalent_qty); 
            $total_remaining_pcs = $total_remaining_pcs % $equivalent_qty;  
        }

        $transfer_out_products[] = array(
            "product_id" => $product_out['prod_id'],
            "product_name" => $product_out['prod_name'],
            "product_qty" => $total_qty,
            "product_pcs" => $total_remaining_pcs
        );

    }
And now I have this $transfer_in_products and $transfer_out_products array separately. I need to deduct the product_out from product_in of every item (each product) to get all of the remaining items.

After doing that I need to add them to my stocks table.
stock_id | stock_prod_name | stock_prod_qty int | stock_prod_pcs_qty |stock_date
Hope this would help everyone to understand my situation.

wyattbiker
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Dec 04, 2017 5:21 am

Mon Dec 04, 2017 7:21 pm

Not sure what you are asking here. Won't nested foreach() work?
or you could organize your in array to use keyed array on product_id.

Code: Select all

<?php
// say your in and out were organized like this
$product_in=array();
$product_in[2]=array('product_qty' => 353,'product_pcs' => 50);
$product_in[8]=array('product_qty' => 100,'product_pcs' => 10);
$product_in[10]=array('product_qty' => 50,'product_pcs' => 8);
$product_in[17]=array('product_qty' => 60,'product_pcs' => 9);

$product_out=array();
$product_out[2]=array('product_qty' => 10,'product_pcs' => 1);
$product_out[5]=array('product_qty' => 20,'product_pcs' => 2);

// then loop over each out
foreach($product_out as $id => $record){
	if (isset($product_in[$id])){
	   $product_in[$id]['product_qty'] -=  $record['product_qty'];
	   $product_in[$id]['product_pcs'] -=  $record['product_pcs'];
	} else {
	   echo "No item $id in inventory\n";
	}
}
var_dump($product_in);
?>

Post Reply