EDIT:  Calculating the difference between 2 arraysI'm coding PHP for only 4 months, and I need your help to improve this codeI have 2 arrays like this//array1 $array1[5] = 20; $array1[6] = 50; //array2 $array2[6] = 60; $array2[7] = 30;Now I need to get the difference between the values of each key between array2 and array1 i.e $array2 - $array1 (if this is legal)So, I need the result array difference to be like this$arraydiff[5] = -20; // NULL - $array1[5]$arraydiff[6] =  10; // $array2[6] - $array1[6]$arraydiff[7] =  30; // $array2[7] - NULLI got this done by doing thatforeach($array2 as $key=> $value){    //check if the key exists only in array2 or in both arrays    if(isset($array1[$key])){$result[$key] = $array2[$key] - $array1[$key];}    else{$result[$key] = $array2[$key];}            }//get the keys that exist in array1 but not in array2$array1diff = array_diff_key($array1,$array2);foreach($array1diff as $key=> $value){     $result[$key] = -1 * $array1[$key];}var_dump($result);this results toarray(3) { [6]=> int(10) [7]=> int(30) [5]=> int(-20) }Is there any better way or PHP built-in tools to get this done instead of that. Can't I do for example $result = $array2 - $array1;?
Calculating the difference between two (incomplete) arrays
php