PHP减数组值 [英] PHP subtract array values

查看:98
本文介绍了PHP减数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个键和值的数组。每个值是一个整数。我有一个其他阵列相同的按键。我怎么能减去所有值的匹配钥匙?也有可能是不发生的第二阵列中,但两个阵列具有相同的长度的密钥。如果在阵列2的关键,是不是在阵列1 present其值应保持不变。如果在第一阵列不在第二应当扔掉一个键。我该怎么做?是否有任何内置的功能呢?

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 there might be keys that do not occur in the second array but both arrays have the same length. If there is a key in array 2 that is not present in array 1 its value should be unchanged. If there is a key in the first array that is not in the second it should be thrown away. How do I do it? Is there any built-in function for this?

如果我会写一个脚本,这将是某种像这样的循环:

If I would write a script it would be some kind of for loop like this:

$arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
$arr2 = array('a' => 2, 'b' => 1, 'c' => 5);
$ret = array();
foreach ($arr1 as $key => $value) {
    $ret[$key] = $arr2[$key] - $arr1[$key];
}
print_r($ret);
/*
should be: array('a' => 1, 'b' => -2, 'c' => -5)
*/

我没有在这里添加际关键是在一个阵列中,而不是在其他的。

I did not add the occasion here a key is in one array and not in the other.

推荐答案

您可以使用阵列功能,如果你是这样的倾向避免在foreach。

You could avoid the foreach using array functions if you were so inclined.

的封闭件所提供的<一href=\"http://us3.php.net/manual/en/function.array-map.php\"><$c$c>array_mapdocs下面将从每个对应 $ ARR2 减去每个 $ ARR1 值。不幸的是 array_map 不会preserve使用多个输入数组时,你的钥匙,所以我们使用<一个href=\"http://www.php.net/manual/en/function.array-combine.php\"><$c$c>array_combinedocs对减去结果合并回到与原来的键数组:

The closure provided to array_mapdocs below will subtract each $arr1 value from each corresponding $arr2. Unfortunately array_map won't preserve your keys when using more than one input array, so we use array_combinedocs to merge the subtracted results back into an array with the original keys:

$arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
$arr2 = array('a' => 2, 'b' => 1, 'c' => 5);

$subtracted = array_map(function ($x, $y) { return $y-$x; } , $arr1, $arr2);
$result     = array_combine(array_keys($arr1), $subtracted);

var_dump($result);

更新

我感兴趣的是阵列功能进场如何比较简单的foreach,所以我用基准既Xdebug的。下面是测试code:

I was interested in how the array functions approach compared to a simple foreach, so I benchmarked both using Xdebug. Here's the test code:

$arr1 = array('a' => 1, 'b' => 3, 'c' => 10);
$arr2 = array('a' => 2, 'b' => 1, 'c' => 5);

function arrayFunc($arr1, $arr2) {
  $subtracted = array_map(function ($x, $y) { return $y-$x; } , $arr1, $arr2);
  $result     = array_combine(array_keys($arr1), $subtracted);
}

function foreachFunc($arr1, $arr2) {
  $ret = array();
  foreach ($arr1 as $key => $value) {
    $ret[$key] = $arr2[$key] - $arr1[$key];
  }
}

for ($i=0;$i<10000;$i++) { arrayFunc($arr1, $arr2); }
for ($i=0;$i<10000;$i++) { foreachFunc($arr1, $arr2); }

事实证明,使用的foreach 循环是一个数量级比实现使用数组功能相同的任务要快。正如你可以从下面的KCacheGrind调试被叫方的图像,所需的阵列功能的方法有近80%的在上面code中的处理时间看,而只需要不到5%的foreach功能。

As it turns out, using the foreach loop is an order of magnitude faster than accomplishing the same task using array functions. As you can see from the below KCachegrind callee image, the array function method required nearly 80% of the processing time in the above code, while the foreach function required less than 5%.

这里的教训:有时候越是语义阵列功能(?奇怪)可差的性能,明智的做法是在PHP中一个良好的老式循环。当然,你应该总是选择更可读/语义的选项;微优化这样如果他们的code更难以理解半年下来,道路是没有道理的。

The lesson here: sometimes the more semantic array functions (surprisingly?) can be inferior performance-wise to a good old fashioned loop in PHP. Of course, you should always choose the option that is more readable/semantic; micro-optimizations like this aren't justified if they make the code more difficult to understand six months down the road.

这篇关于PHP减数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆