I have an array with keys and values. Each value is an integer. I have an other array with the same keys. How can I subtract all of the values for the matching keys? Also th
PHP does not offer vectorized mathematical operations. I would stick with your current approach of using a loop.
First, I would get the set of array keys for each array. (See the array_keys
function). Then, intersect them. Now you will have the keys common to each array. (Take a look at the array_intersect
function). Finally, iterate. It's a readable and simple approach.
Lastly, you could take a look at a library, such as Math_Vector: http://pear.php.net/package/Math_Vector