Home > Software engineering >  how to apply mathematical operations between two array's identical elements(php)
how to apply mathematical operations between two array's identical elements(php)

Time:03-20

I'm trying to subtract the wight and quantity of elements in two array if the id exist in both array. first array called sales

    [0] => Array
        (
            [id] => 13
            [name] => Rahul
            [sale_qty] => 2
            [sale_weight] => 38.00
        )

    [1] => Array
        (
            [id] => 14
            [name] => shravandhika
            [sale_qty] => 1
            [sale_weight] => 12.00
        )

    [2] => Array
        (
            [id] => 3
            [name] => veena
            [sale_qty] => 1
            [sale_weight] => 19.00
        )

)

second array called returns

Array
(
    [0] => Array
        (
            [id] => 13
            [name] => Rahul
            [sale_qty] => 1
            [sale_weight] => 8.00
        )

    [1] => Array
        (
            [id] => 7
            [name] => sree
            [sale_qty] => 1
            [sale_weight] => 22.00
        )

)

i'm trying to get new array that have all id of the two array and sale_qty and sale_weight should be subtracted if the id exist in both array, means the resultant array should be


[0] => Array
        (
            [id] => 13
            [name] => Rahul
            [sale_qty] => 1
            [sale_weight] => 30.00
        )

    [1] => Array
        (
            [id] => 14
            [name] => shravandhika
            [sale_qty] => 1
            [sale_weight] => 12.00
        )

    [2] => Array
        (
            [id] => 3
            [name] => veena
            [sale_qty] => 1
            [sale_weight] => 19.00
        )
    [3] => Array
        (
            [id] => 7
            [name] => sree
            [sale_qty] => 1
            [sale_weight] => 22.00
        )

)

here the id 13's sale_qty and sale_weight are subtracted and here is my try


    echo "<pre>"; print_r($sales); echo"</pre>"; //sales array
    echo "<pre>"; print_r($returns); echo"</pre>"; //return array
    $result_array = array();
    foreach ($sales as $salekey=> $salevalue){      
        foreach ($returns as $retkey=> $retvalue){
    
            if($retvalue['id'] == $salevalue['id']){
                $salevalue['despatch_qty']= $retvalue['despatch_qty'] - $salevalue['despatch_qty'];
                $salevalue['despatch_weight'] =  $retvalue['despatch_weight'] - $salevalue['despatch_weight'];  
            }
            
        }
        $result_array[]= array('id' => $salevalue['id'],'name'=> $salevalue['name'],'qty'=> $salevalue['despatch_qty'],'weight'=> $salevalue['despatch_weight']);
    }

this didn't give the all ids please let me know the easiest and optimised method to do this

CodePudding user response:

sorry I'm also new to PHP

the method is almost the same as your script but I added unset() in some exceptions and some tweaks to my version :)

below is the same script with the output you want :)

//php 7.3.0
$arr1 = [
    [
        "id" => 13,
        "name" => "Rahul",
        "sale_qty" => 2,
        "sale_weight" => 38.00
    ],

    [
        "id" => 14,
        "name" => "shravandhika",
        "sale_qty" => 1,
        "sale_weight" => 12.00
    ],
    [
        "id" => 3,
        "name" => "veena",
        "sale_qty" => 1,
        "sale_weight" => 19.00
    ]
];

$arr2 = [
       [
        "id" => 13,
        "name" => "Rahul",
        "sale_qty" => 1,
        "sale_weight" => 8.00
       ],

       [
        "id" => 17,
        "name" => "sree",
        "sale_qty" => 1,
        "sale_weight" => 22.00
       ]
];

$NewArr = [];

foreach($arr1 as $key1 => $value1)
{

  foreach($arr2 as $key2 => $value2)
  {
  
    if($value1["id"] == $value2["id"])
    {
      $value1["sale_weight"] -= $value2["sale_weight"];
      unset($arr2[$key2]);
    } 
    else 
    {
      $NewArr[] = $value2;
      unset($arr2[$key2]);
    } 
}
$NewArr[] = $value1;
}

//output
print_r($NewArr);

CodePudding user response:

You could merge both arrays, and use the id key as the key for a resulting array.

Then check if the key already exists in the result. If it does not, add the item, else take the already existing item by id and subtract the values.

$sales = [
    ["id" => 13, "name" => "Rahul", "sale_qty" => 2, "sale_weight" => 38.00], 
    ["id" => 14, "name" => "shravandhika", "sale_qty" => 1, "sale_weight" => 12.00], 
    ["id" => 3, "name" => "veena", "sale_qty" => 1, "sale_weight" => 19.00]
];

$returns = [
    ["id" => 13, "name" => "Rahul", "sale_qty" => 1, "sale_weight" => 8.00],
    ["id" => 7, "name" => "sree", "sale_qty" => 1, "sale_weight" => 22.00]
];

$result = [];
$items = array_merge($sales, $returns);

foreach($items as $item) {
    $saleId = $item['id'];
    if (array_key_exists($saleId, $result)) {
        $result[$saleId]['sale_qty'] -= $item['sale_qty'];
        $result[$saleId]['sale_weight'] -= $item['sale_weight'];
    } else {
        $result[$saleId] = $item;
    }
}

print_r($result);

Output

Array
(
    [13] => Array
        (
            [id] => 13
            [name] => Rahul
            [sale_qty] => 1
            [sale_weight] => 30
        )

    [14] => Array
        (
            [id] => 14
            [name] => shravandhika
            [sale_qty] => 1
            [sale_weight] => 12
        )

    [3] => Array
        (
            [id] => 3
            [name] => veena
            [sale_qty] => 1
            [sale_weight] => 19
        )

    [7] => Array
        (
            [id] => 7
            [name] => sree
            [sale_qty] => 1
            [sale_weight] => 22
        )

)

See a PHP demo.

  • Related