jeudi 29 juin 2017

Merge array by key with conditions

I have a problem. I would like to merge array by conditions. If array keys match then add its value if not then print as it is. I tried all the possible thing I can but didn't get the right result. here is my arrays.............

 Array1(   [1] => 199
           [3] => 1306
           [5] => 199
         )
  Array2(  [3] => 199
           [4] => 199
           )

I want to add the value if array keys match and print as it is if not match..like this...

  Resylt(  [1] => 199
           [3] => 1505
           [4] => 199
           [5] => 199    
           )

I used if else conditions. but its repeating the value which is already match here is my code...

  $all=array(); 
  foreach($sall as $sskey => $ssvalue){
     foreach($upgradesall as $uukey => $uuvalue){
         //$sskey==$uukey?$all[] = array("id"=>$sskey, "amount"=>$ssvalue+$uuvalue):($sskey!=$uukey? $all[] = array("id"=>$sskey, "amount"=>$ssvalue):($uukey!=$sskey?$all[] = array("id"=>$uukey, "amount"=>$uuvalue):''));
         if($sskey===$uukey){
             $all[] = array("id"=>$sskey, "amount"=>$ssvalue+$uuvalue);
         }
         elseif($sskey!=$uukey){
             $all[] = array("id"=>$sskey, "amount"=>$ssvalue);
         }
         elseif($uukey!=$sskey){
             $all[] = array("id"=>$uukey, "amount"=>$uuvalue);
         }

         }
        }   

Aucun commentaire:

Enregistrer un commentaire