First, you either have to make the second array searchable by using the Id
value as the keys or write a search function. I'm choosing the former:
$searchable = array_reduce($array2, function(&$result, $item) {
return $result + array($item['Id'] => $item['row1']);
}, array());
The array_reduce()
function starts with an empty array and builds it using the array addition operator; this creates an array that can be dereferenced using the id.
Then you perform a map operation on the first array:
$array3 = array_map(function($item) use ($searchable) {
$value = isset($searchable[$item['Id']]) ? $searchable[$item['Id']] : 0;
$item['row1'] -= $value;
// $item['row2'] = $item['row1'] - $value;
return $item;
}, $array1);
Doing a map operation preserves the original array and creates a new one with the values you choose inside a callback function.