如何比较2个数组与相同的键但不同的值PHP

时间:2014-05-11 05:47:37

标签: php arrays

我想询问比较2个具有相同键但不同值的数组。

我有1个阵列主控(arrayMaster)和2个或更多阵列数据(arrayData1arrayData2,可能还有更多)。这些数组数据键将只有一个arrayMaster数据键(我已为此事做过)。对于我得到的数据示例:

arrayMaster = Array( [apple] => 1 [banana] => 2 [choco] => 1 [donut] => 2 [egg] => 1 )
arrayData1 = Array( [apple] => 8 [banana] => 2 [choco] => 1 )
arrayData2 = Array( [donut] => 5 [choco] => 2 [egg] => 3 ) 

(我们可以看到arrayData1arrayData2包含来自arrayMaster密钥的密钥。)

我想比较这些数组并给出一个计算方法。如果arrayData(n)处的数组键位于arrayMaster,它将执行一个计算数据,让它说相互相加。

所以,结果是:

arrayResult1 = 1+8 (apple have 1 from master, apple have 8 from arrayData1), 2+2, 1+1
arrayResult2 = 2+5 (donut have 2 from master, donut have 5 from arrayData2), 1+2, 1+3

所以我将有2个新数组(或更多,取决于arrayData的数量)包含:

arrayResult1 = ([apple] => 9 [banana] => 4 [choco] => 2);
arrayResult2 = ([donut] => 7 [choco] => 3, [egg] => 4);

任何人都知道如何做到这一点?我已经尝试了array_intersect,但它没有用。

3 个答案:

答案 0 :(得分:3)

做这样的事情:

function doCalc($master, $slave) {
    $results = array();

    foreach( $slave as $key => $value ) {
        if( !isset($master[$key]) ) {
            $results[$key] = $value;
        }
        else {
            $results[$key] = $master[$key] + $value;
        }
    }

    return $results;
}

$arrayResult1 = doCalc($arrayMaster, $arrayData1);
$arrayResult2 = doCalc($arrayMaster, $arrayData2);

答案 1 :(得分:2)

你可以写一些更简单的东西......

function modifyArr(&$arr,$basearray) //<=-- See I am passing & (reference) so your original array will be modified
{
foreach($arr as $k=>$v)
 {
    if(array_search($k,$basearray)!==null)
    {
        $arr[$k]=$basearray[$k]+$arr[$k];
    }
 }
}

modifyArr($arrayData1,$arrayMaster); //<=-- Pass your 1st array
modifyArr($arrayData2,$arrayMaster); //<=-- Pass your 2nd array

Demonstration

答案 2 :(得分:0)

以这些为例:

arrayResult1 = 1+8 (apple have 1 from master, apple have 8 from arrayData1), 2+2, 1+1
arrayResult2 = 2+5 (donut have 2 from master, donut have 5 from arrayData2), 1+2, 1+3

为什么不这样做:

// The main arrays for master & data values.
$arrayMaster = array('apple' => 1, 'banana' => 2, 'choco' => 1, 'donut' => 2, 'egg' => 1);
$arrayData1 = array('apple' => 8, 'banana' => 2, 'choco' => 1);
$arrayData2 = array('donut' => 5, 'choco' => 2, 'egg' => 3);

// Set a values to check array.
$values_to_check = array('apple', 'donut');

// Init the results array.
$results_array = array();

// Roll through the values to check.
foreach ($values_to_check as $value) {

  // Check if the array key exists in '$arrayMaster'.
  if (array_key_exists($value, $arrayMaster)) {

    // If it exists, add it to the '$results_array'.
    $results_array[$value][] = $arrayMaster[$value];

    // Check if the array key exists in '$arrayData1'.
    if (array_key_exists($value, $arrayData1)) {
      // If it exists, add it to the '$results_array'.
      $results_array[$value][] = $arrayData1[$value];
    }

    // Check if the array key exists in '$arrayData2'.
    if (array_key_exists($value, $arrayData2)) {
      // If it exists, add it to the '$results_array'.
      $results_array[$value][] = $arrayData2[$value];
    }

  }

}

// Roll through the results array and use 'array_sum' to get a sum of values.
foreach ($results_array as $results_key => $results_value) {
  echo $results_key . ' : ' . array_sum($results_value) . '<br />';
}

但是看看你的例子,我不清楚为什么你有$arrayData1$arrayData2的单独数组,所以这里是相同的代码,但重构为在$arrayData中有嵌套数组应该更有效率:

// The main arrays for master & data values.
$arrayMaster = array('apple' => 1, 'banana' => 2, 'choco' => 1, 'donut' => 2, 'egg' => 1);
$arrayData = array();
$arrayData[] = array('apple' => 8, 'banana' => 2, 'choco' => 1);
$arrayData[] = array('donut' => 5, 'choco' => 2, 'egg' => 3);

// Set a values to check array.
$values_to_check = array('apple', 'donut');

// Init the results array.
$results_array = array();

// Roll through the values to check.
foreach ($values_to_check as $value) {

  // Check if the array key exists in '$arrayMaster'.
  if (array_key_exists($value, $arrayMaster)) {

    // If it exists, add it to the '$results_array'.
    $results_array[$value][] = $arrayMaster[$value];

    // Roll through the values to check.
    foreach ($arrayData as $arrayData_value) {
      // Check if the array key exists in '$arrayData1'.
      if (array_key_exists($value, $arrayData_value)) {
        // If it exists, add it to the '$results_array'.
        $results_array[$value][] = $arrayData_value[$value];
      }
    }

  }

}

// Roll through the results array and use 'array_sum' to get a sum of values.
foreach ($results_array as $results_key => $results_value) {
  echo $results_key . ' : ' . array_sum($results_value) . '<br />';
}