PHP subtract array values

后端 未结 3 1543
臣服心动
臣服心动 2020-12-06 12:22

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

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 13:02

    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

提交回复
热议问题