Zaz
Zaz

Reputation: 1102

How to compare 2 arrays and push new values

How to compare 2 arrays and push new values.

Perhaps the title is wrong, but i did not know what to write, i hope it is okay.

This is my first array. It contains european zipcode and how many members there is in the zipcode

Array1 called $Memcooltransactions

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

This is my second array. It also contains european zipcode and how many restaurants there is in the zipcode

Array2 called $Rescooltransactions

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

This is my code to compare these arrays together.

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, 
        );  

    }
  }
}

My output.

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

When I am trying to grab the values ​​that could not compare into a new array, it crashes the site. That is why I deleted it from my foreach above. But how can I get the desired output without crashing the site?

Desired output

 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 )
) 

Hope you can help :)

Upvotes: 2

Views: 367

Answers (3)

Emissary
Emissary

Reputation: 10148

A few minutes late but I'll post this anyway as it's scalable. I made some assumptions:

  • City is unique
  • City can have multiple zipcodes
  • keys with integer in both arrays are summed
  • otherwise values in $arr2 take precidence

-

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

Upvotes: 0

Toto
Toto

Reputation: 91428

You have to do two loops, the first one to initialize the result array with the data in the first array, and a second loop to add data that are in the second array but not in the first.

Something like:

$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);

output:

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
        )

)

Upvotes: 1

Alec Sanger
Alec Sanger

Reputation: 4562

Give this a try. It will merge the two arrays into $newArray and will not include duplicates.

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

Upvotes: 0

Related Questions