lundi 11 janvier 2021

how to compare 2 array and compare difference of same id in php?

this questin is asked many times but every one using same array but in my case i have 2 arrays consider i have 2 arrays

array1:3 [
  10 => 900.0
  20 => 450.0
  30 => 600.0
]

array2:3 [
  30 => 200.0
  10 => 500.0
  20 => 600.0
]

output should be

[900.0 - 500 = 400   // according to same id 10 = 10
 450.0 - 600 = -150  //                       20 = 20
 600.0 - 200 = 400  //                        30 = 30
]

in this array consider 10,20,30 are ids and next is value i want output where compare ever id and get difference example if (id1 = id2 ){ id1 => value - id2 => value } i need help in that code which i already tried

$getsellerreport = SellerSellsReport::where('seller_id' , $seller_id);
             $getunitdiff = $getsellerreport->pluck('unit')->toArray();// [0 => 75 1 => 500 => 100]
             $getamountdiff = $getsellerreport->pluck('amount')->toArray(); // [0 => 11000 => 40 2 => 900]
             $getproductdiff = $getsellerreport->pluck('product_id')->toArray(); // [0 => 39 1 => 242 => 23]
             
             foreach($product_report as $preport){
                $unit[] = $preport['unit'];// [0 => 75 1 => 25 2 => 100]
                $amount[] = $preport['amount'];// [0 => 900 1 => 450 2 => 600]
                $product_id[] = $preport['product_id'];// [0 => 23 1 => 242 => 39]
               
                } // here we get array two values

above code get values with starting 0 key value and on below for() loop we can use product_id to compare both product id and get unit and amount but i dont know how i can do that can someone help me?

for ($i = 0 ; $i < sizeof($amount) ; $i++){
                $unitdiff[] = $getunitdiff[$i] - $unit[$i];
                $amountdiff[] = $getamountdiff[$i] - $amount[$i];
            }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire