0

How can I merge arrays witch have the same key 'email'. Like this exemple [4] and [6] have the same email adress.

Array
(
    [0] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => Canvas
            [firstname] => Peter
            [birthdate] => 1987-09-13
            [country] => DE
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => yes
            [date] => 2011-08-11 15:41:54
            [vol6t3] => L'homme de bernadette
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [1] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => Canvas
            [firstname] => Peter
            [birthdate] => 1987-09-13
            [country] => DZ
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => yes
            [date] => 2011-08-11 15:42:50
            [vol6t3] => L'homme de bernadette
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [2] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => qsd
            [firstname] => qsd
            [birthdate] => 1981-01-17
            [country] => AT
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => 
            [date] => 2011-08-11 15:43:22
            [vol6t3] => Un militant de gauche
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [3] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => qsd
            [firstname] => qsdqsd
            [birthdate] => 1982-02-19
            [country] => BS
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => 
            [date] => 2011-08-11 15:45:13
            [vol6t3] => Un militant de gauche
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [4] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => qds
            [firstname] => dqs
            [birthdate] => 1980-11-19
            [country] => BS
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => 
            [date] => 2011-08-11 15:46:14
            [vol6t3] => Un militant de gauche
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [5] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => Canvas
            [firstname] => Peter
            [birthdate] => 1987-09-13
            [country] => DE
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => yes
            [date] => 2011-08-11 15:47:11
            [vol6t3] => L'homme de bernadette
            [uwwpma] => 
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 1
        )

    [6] => Array
        (
            [email] => [email protected]
            [gender] => m
            [lastname] => Canvas
            [firstname] => Peter
            [birthdate] => 1987-09-13
            [country] => DE
            [xgfx3r] => 
            [language] => fr_FR
            [newsletter] => 0
            [newsletter_partner] => 0
            [facebook_login] => yes
            [date] => 2011-08-11 15:48:35
            [vol6t3] => 
            [uwwpma] => Toi
            [bhqgzn] => 
            [nb_email_shared] => 
            [period] => 2
        )

)

into

Array
    (
        [0] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => Canvas
                [firstname] => Peter
                [birthdate] => 1987-09-13
                [country] => DE
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => yes
                [date] => 2011-08-11 15:41:54
                [vol6t3] => L'homme de bernadette
                [uwwpma] => 
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 1
            )

        [1] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => Canvas
                [firstname] => Peter
                [birthdate] => 1987-09-13
                [country] => DZ
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => yes
                [date] => 2011-08-11 15:42:50
                [vol6t3] => L'homme de bernadette
                [uwwpma] => 
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 1
            )

        [2] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => qsd
                [firstname] => qsd
                [birthdate] => 1981-01-17
                [country] => AT
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => 
                [date] => 2011-08-11 15:43:22
                [vol6t3] => Un militant de gauche
                [uwwpma] => 
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 1
            )

        [3] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => qsd
                [firstname] => qsdqsd
                [birthdate] => 1982-02-19
                [country] => BS
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => 
                [date] => 2011-08-11 15:45:13
                [vol6t3] => Un militant de gauche
                [uwwpma] => 
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 1
            )

        [4] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => qds
                [firstname] => dqs
                [birthdate] => 1980-11-19
                [country] => BS
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => 
                [date] => 2011-08-11 15:46:14
                [vol6t3] => Un militant de gauche
                [uwwpma] => Toi
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 2
            )

        [5] => Array
            (
                [email] => [email protected]
                [gender] => m
                [lastname] => Canvas
                [firstname] => Peter
                [birthdate] => 1987-09-13
                [country] => DE
                [xgfx3r] => 
                [language] => fr_FR
                [newsletter] => 0
                [newsletter_partner] => 0
                [facebook_login] => yes
                [date] => 2011-08-11 15:47:11
                [vol6t3] => L'homme de bernadette
                [uwwpma] => 
                [bhqgzn] => 
                [nb_email_shared] => 
                [period] => 1
            )
    )

Many thanks before !

regard, Peter

1
  • 2
    [4] and [6] have the same email address, but different data. How do you know which data to keep and which to discard? Commented Aug 17, 2011 at 15:25

5 Answers 5

1

If I understood your example correctly, after you've got one entry with email [email protected], you need to discard anything following with the same email.

That should do it:

$result = array();
foreach ($arr as $entry) {
    if (!isset($result[$entry['email']])) {
        $result[$entry['email']] = $entry;
    }
}
$result = array_values($result);
Sign up to request clarification or add additional context in comments.

Comments

0

something like this (not tested, just for example):

foreach( $first_array as $key => &$value) {
  if ($value['email'] == $second_array[$key]['email']) {
    array_merge($value, $second_array[$key]);
  }
}

Comments

0
$merged = []
foreach($arr as $v){
    $e = $v['email'];
    if(array_key_exists($merged[$e]){
        $merged[$e] = array_merge($v,$merged[$i]);
    }else{
        $merged[$e] = $v;
    }
}

// Merged will now be an associative array with the e-mail addresses as keys
// if you want to use it as a normal array use 
$merged = array_values($merged);

1 Comment

@Nimbus147: I don't understand your question
0

It's quite easy to find the member arrays with the same email address and 'merge' them, the challenge is to decide how the other fields are overwritten.

If you use array_merge() the later values will always take precedence, but this may not be what you want - in the example you provided, the resulting array has some values from the first array and some from the second (the 'vol6t3' key has the value from the first array, but the 'lastname' and 'firstname' keys are taken from the second).

Off the top of my head you will have to loop through the data to collect a list of all the articles with the same email address, then loop through that list and merge the data based on a per-field decision that I really can't help you with.

Some of these would be easy - if the field has data in one array and no data in the other (like the 'vol6t3' key) you would take the one with data. But if the field has data in both arrays, you would have to decide which data to keep and which to discard (or you could keep both and turn the field into an array).

I will provide a code example if you like but it will take me some time to write it...

Comments

0

Thanks all and special thank to Stanislav Shabalin who really help me.

I write my function which run successfully :) bellow :

    /**
       * Get a distinct multi dimensional array
       * 
       * @param array $array
       *  array to distinct
       * @param string $keySearch
       *  string key to search double array
       * @param boolean $overwrite = false
       *  boolean to allow values to be overwritten
       * @param array $exception = array()
       *  array of keys to not overwrite if overwrite is true
       * 
       * @return array $result
       *  Return a multi dimensional array witch has been distinct.
       */
    public static function distinctMultiDimensionalArray($array, $keySearch, $overwrite = false, $exception = array())
    {

      // Check if it's an array
      if( !is_array($array) )
        return false;

      $result = array();

      foreach ( $array as $entry ) 
      {
        // If email doesn't exist
        if ( !isset($result[$entry[$keySearch]]) ) 
          $result[$entry[$keySearch]] = $entry;
        else 
        {
          // If email exist
          foreach ( $entry as $key => $value ) 
          {
            if( !empty($value) )
            {
              // If not empty value and this value is different from before and you don't want to overwrite values
              // Or you want to overwrite values except some keys
              if( ( !empty( $result[$entry[$keySearch]][$key] ) 
                    && $result[$entry[$keySearch]][$key] != $value 
                    && $overwrite == false )
                ||
                  ( $overwrite == true 
                    && in_array($key, $exception) ) )
                $result[$entry[$keySearch]][$key] = $result[$entry[$keySearch]][$key] . ', ' . $value;
              else
                $result[$entry[$keySearch]][$key] = $value;
            }


          }
        }
      }
      $result = array_values($result);

      return $result;
    }

Thanks a lot !

1 Comment

If @Stanislav Shabalin has helped you a lot, then you should accept his answer. That's how the OP show special thanks and give back to SO Community.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.