Jump to content

removing duplicates of pairs of values based on matching keys


phdphd
Go to solution Solved by Muddy_Funster,

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 !

Link to comment
Share on other sites

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
        )

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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.";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.