如何比较2个数组并推送新值

时间:2013-03-19 10:50:20

标签: php arrays

如何比较2个数组并推送新值。

也许标题是错的,但我不知道写什么,我希望没关系。

这是我的第一个阵列。它包含欧洲邮政编码以及邮政编码中有多少成员

Array1名为$ Memcooltransactions

    Array ( 
[0] => Array ( [zipcode] => 1067 [city] => Copenhagen K [numberofuniqmembers] => 11 ) 
[1] => Array ( [zipcode] => 0100 [city] => Tórshavn [numberofuniqmembers] => 1 )
)

这是我的第二个阵列。它还包含欧洲邮政编码和邮政编码中有多少家餐馆

Array2名为$ Rescooltransactions

Array ( 
[0] => Array ( [zipcode] => 1067 [city] => Copenhagen K [numberofuniqplaces] => 1) 
[1] => Array ( [zipcode] => 1074 [city] => København V [numberofuniqplaces] => 1 )
)

这是我将这些数组进行比较的代码。

foreach ($Memcooltransactions as $key) 
{
    $uniqmember = $key['numberofuniqmembers'];

  foreach ($Rescooltransactions as $key2) 
  {
    $uniqres = $key2['numberofuniqspisesteder'];

    if($key['zipcode'] === $key2['zipcode'])
    {

        $samlet = $uniqmember/$uniqres;
        $sizeofuniqmem += $uniqmember;
        $sizeofuniqres += $uniqres;

        $array1[] = array(
        'zipcode' => $key['zipcode'],
        'city' => $key['city'],
        'numberofuniqmembers' => $uniqmember,
        'numberofuniqspisesteder' => $uniqres, 
        );  

    }
  }
}

我的输出。

Array (     [0] => Array ( [zipcode] => 1067 [city] => Copenhagen K [numberofuniqmembers] => 11 [numberofuniqplaces] => 1 ) 

当我试图获取无法与新数组进行比较的值时,它会崩溃网站。这就是我从上面的foreach中删除它的原因。但是,如何在不崩溃网站的情况下获得所需的输出?

期望的输出

 Array ( 
[0] => Array ( [zipcode] => 1067 [city] => Copenhagen K [numberofuniqmembers] => 11 [numberofuniqplaces] => 1 ) 
[1] => Array ( [zipcode] => 0100 [city] => Tórshavn [numberofuniqmembers] => 1 [numberofuniqplaces] => 0 )
[2] => Array ( [zipcode] => 1074 [city] => København V [numberofuniqmembers] => 0 [numberofuniqplaces] => 1 )
) 

希望你能提供帮助:)

3 个答案:

答案 0 :(得分:1)

你必须做两个循环,第一个用第一个数组中的数据初始化结果数组,第二个循环用来添加第二个数组但不在第一个数组中的数据。

类似的东西:

$result = array();
// first loop, initialize $result
foreach ($Mem as $key) {
    $result[] = array(
        'zipcode' => $key['zipcode'],
        'city' => $key['city'],
        'numberofuniqmembers' => $key['numberofuniqmembers'],
        'numberofuniqplaces' => 0, 
    );  
}
$create = 1;
// second loop, complete $result
foreach ($Res as $key) {
    $uniqres = $key['numberofuniqplaces'];
    $i = 0;
    foreach ($result as $key2) {
        $uniqmember = $key2['numberofuniqmembers'];
        if($key['zipcode'] === $key2['zipcode']) {
            $result[$i] = array(
                'zipcode' => $key['zipcode'],
                'city' => $key['city'],
                'numberofuniqmembers' => $uniqmember,
                'numberofuniqplaces' => $uniqres, 
            );
            $create = 1;
            break; // <--- modified
        } elseif ($create) {
            $create = 0;
            $result[] = array(
                'zipcode' => $key['zipcode'],
                'city' => $key['city'],
                'numberofuniqmembers' => 0,
                'numberofuniqplaces' => $uniqres, 
            );  
        }
    }
    $i++;
}
print_r($result);

<强>输出:

Array
(
    [0] => Array
        (
            [zipcode] => 1067
            [city] => Copenhagen K
            [numberofuniqmembers] => 11
            [numberofuniqplaces] => 1
        )

    [1] => Array
        (
            [zipcode] => 0100
            [city] => Tórshavn
            [numberofuniqmembers] => 1
            [numberofuniqplaces] => 0
        )

    [2] => Array
        (
            [zipcode] => 1074
            [city] => København V
            [numberofuniqmembers] => 0
            [numberofuniqplaces] => 1
        )

)

答案 1 :(得分:0)

试一试。它会将两个数组合并为$newArray,并且不会包含重复项。

$newArray = Array();
foreach($arr1 as $k=>$val)
{
    if(array_key_exists($k, $arr2))
    {
        $newArray[$k] = array_merge($val, $arr2[$k]);
    }
}

答案 2 :(得分:0)

迟了几分钟,但我会发布这个,因为它可以扩展。我做了一些假设:

  • 城市是独一无二的
  • City可以有多个zipcodes
  • 两个数组中integer的键总和
  • 否则$arr2中的值会采取预防措施

-

function mergeLocations($arr1, $arr2){
  $tmp = array(); $keys = array(); $ret = array();
  foreach($arr1 as $loc) 
    foreach($loc as $key => $val)
      if(isset($loc['city'])){
        if(!isset($tmp[$loc['city']]))
          $tmp[$loc['city']] = array();
        $tmp[$loc['city']][$key] = $val;
        $keys[] = $key;
      }
  foreach($arr2 as $loc) 
    foreach($loc as $key => $val)
        if(isset($loc['city'])){
          if(!isset($tmp[$loc['city']]))
            $tmp[$loc['city']] = array();
          $keys[] = $key; 
        }
  $keys = array_unique($keys);
  foreach($arr2 as $loc)
    foreach($loc as $key => $val)
      if(isset($loc['city']))
        if(!isset($tmp[$loc['city']][$key]) || !is_int($tmp[$loc['city']][$key]))
          $tmp[$loc['city']][$key] = $val;
        else 
          $tmp[$loc['city']][$key] += $val;
  foreach($tmp as $key => $loc){
    foreach($keys as $k)
      if(!isset($loc[$k]))
        $loc[$k] = 0;
    $ret[] = $loc;
  }
  return $ret; 
}

Working Example