Jump to content

removing duplicates of pairs of values based on matching keys


phdphd

Recommended Posts

Hi All,

 

I'm wondering whether there is a quick way to go from

   [countries] => Array
        (
            [0] => Spain
            [1] => France
            [2] => France
            [3] => USA
        )

    [cities] => Array
        (
            [0] => Madrid
            [1] => Paris
            [2] => Paris
            [3] => Boston
        )

to

[countries] => Array
        (
            [0] => Spain
            [1] => France
            [3] => USA
        )

    [cities] => Array
        (
            [0] => Madrid
            [1] => Paris
            [3] => Boston
        )

Actually, it is not important that keys are preserved.

 

Thanks a lot !

Sorry, my starting arrays were too simple. Let assume the following starting arrays instead. In this example, applying array_unique to each array would result in a 3-value array for countries and in a 4-value array for cities.

 [countries] => Array
        (
            [0] => Spain
            [1] => France
            [2] => France
            [3] => France
            [4] => USA
        )

    [cities] => Array
        (
            [0] => Madrid
            [1] => Paris
            [2] => Paris
            [3] => Bordeaux
            [4] => Boston
        )

 

Sorry, my starting arrays were too simple. Let assume the following starting arrays instead. In this example, applying array_unique to each array would result in a 3-value array for countries and in a 4-value array for cities.

 [countries] => Array
        (
            [0] => Spain
            [1] => France
            [2] => France
            [3] => France
            [4] => USA
        )

    [cities] => Array
        (
            [0] => Madrid
            [1] => Paris
            [2] => Paris
            [3] => Bordeaux
            [4] => Boston
        )

Which is actualy exactly what you asked for.

In the last example, the result I am looking for is :

 [countries] => Array
        (
            [0] => Spain
            [1] => France
            [3] => France
            [4] => USA
        )

    [cities] => Array
        (
            [0] => Madrid
            [1] => Paris
            [3] => Bordeaux
            [4] => Boston
        )

In other words, only [2] => France and [2] => Paris are removed because they represent the only pair of country/city values that appears more than once.

so you don't want to remove duplicate entries from an array, you want to remove duplicate entries from a series of concatenated array strings generated from two completly unconnected array entities.

 

I'll tell you right now - you have designed this wrong.

 

Anyway, here:

$array1 = Array('Spain', 'France', 'France', 'France', 'USA');
$array2 = Array('Madrid', 'Paris', 'Paris', 'Bordeaux', 'Boston');
if(count($array1) === count($array2)){
  $compArray = array();
  $tot = 1;
  $matches = 0;
  foreach($array1 as $key =>$val){
    $compArray[$key] = $val.$array2[$key];
  }
  $pairedArray = array_unique($compArray);
  foreach ($compArray as $cK=>$cV){
    if(array_key_exists($cK, $pairedArray)){
      $tot++;
    }
    else{
      unset($array1[$cK]);
      unset($array2[$cK]);
      $tot++;
      $matches++;
    }
  }
  echo "Comparison Compete, $matches duplicates found in $tot items.";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.