0

I have an associative multidimensional array:

Array
(
    [0] => Array
        (
            [customer_name] => John Dow
            [customer_email] => john@example.com
            [customer_mobile] => 1236547895
            [birth_date] => 12/1/1996
            [status] => Enable
        )

    [1] => Array
        (
            [customer_name] => Alex
            [customer_email] => alex@example.com
            [customer_mobile] => 4563214785
            [birth_date] => 19/1/1996
            [status] => Enable
        )

    [2] => Array
        (
            [customer_name] => Arina
            [customer_email] => arina@example.com
            [customer_mobile] => 963214785
            [birth_date] => 25/1/1996
            [status] => Enable
        )

    [3] => Array
        (
            [customer_name] => Atom
            [customer_email] => atom@example.com
            [customer_mobile] => 5214789632
            [birth_date] => 12/1/1998
            [status] => Enable
        )

    [4] => Array
        (
            [customer_name] => Jennifer
            [customer_email] => jennifer@example.com
            [customer_mobile] => 4563214785
            [birth_date] => 12/2/1996
            [status] => Enable
        )
)

Now I want to inspect similar values in customer_mobile and customer_email from each other to reduce redundancies. Contact number and email addresses must be non-redundant.

So please guide me, how can I achieve this? Thanks :)

FirstOne
  • 6,033
  • 7
  • 26
  • 45
Devendra Singh
  • 99
  • 1
  • 2
  • 13

6 Answers6

4

Since you don't need to know which, but only if, you could use array_column + array_unique: (run)

$cm = array_column($arr, 'customer_mobile');
if($cm != array_unique($cm)){
    echo 'There are duplicates in customer_mobile';
}

$ce = array_column($arr, 'customer_email');
if($cm != array_unique($ce)){
    echo 'There are duplicates in customer_email';
}

If you need to match both email and mobile, do it in the same if:

if($cm != array_unique($cm) && $ce != array_unique($ce)){
    echo 'There are duplicates in both customer_mobile and customer_email';
}
FirstOne
  • 6,033
  • 7
  • 26
  • 45
1

Simple solution is:

<?php

$data = [
  [
    'name' => 'name 1',
    'phone' => '12341234',
    'email' => 'test@web.com'
  ],
  [
    'name' => 'name 2',
    'phone' => '12341234',
    'email' => 'test@web1.com'
  ],
  [
    'name' => 'name 3',
    'phone' => '4322342',
    'email' => 'test@web1.com'
  ],
  [
    'name' => 'name 4',
    'phone' => '1234123423',
    'email' => 'test@web1.com'
  ],
  [
    'name' => 'name 5',
    'phone' => '12341266634',
    'email' => 'test@eqweqwweb.com'
  ],
];

$phones = [];
$emails = [];
foreach ($data as $key => $contact) {
  if (array_search($contact['phone'], $phones) !== false || array_search($contact['email'], $emails) !== false) {
    unset($data[$key]);
  } else {
    $phones[] = $contact['phone'];
    $emails[] = $contact['email'];
  }
}

var_dump($data);

and in result you'll get:

array(3) {
  [0] =>
  array(3) {
    'name' =>
    string(6) "name 1"
    'phone' =>
    string(8) "12341234"
    'email' =>
    string(12) "test@web.com"
  }
  [2] =>
  array(3) {
    'name' =>
    string(6) "name 3"
    'phone' =>
    string(7) "4322342"
    'email' =>
    string(13) "test@web1.com"
  }
  [4] =>
  array(3) {
    'name' =>
    string(6) "name 5"
    'phone' =>
    string(11) "12341266634"
    'email' =>
    string(18) "test@eqweqwweb.com"
  }
}

this is just example.

kRicha
  • 797
  • 9
  • 27
0

You can do it in this way (i generate code from head so it can have bug - but idea should be clear) (i assume you array name is $persons):

$emails = [];
$mobiles = [];

$discard = false;
foreach($persons as $person) 
{
   $email = $person['customer_email'];

   if(!isset($emails[$email])) {
       $emails[$email] = $person;
   } else {
      $emails[$email]['redundant_email']=true;
      $person['redundant_email']=true;
      $discard = true;
   }

   $mobile = $person['customer_mobile'];

   if(!isset($mobiles[$mobile])) {
       $mobiles[$mobile] = $person;
   } else {
       $mobiles[$mobile]['redundant_mobile']=true;
       $person['redundant_mobile']=true;
       $discard = true;
   }
}

As result each person with redundat mobile or email have set field redundant_email or redundant_mobile to true. The variable $discard=true says that array is redundant.

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

Try this with foreach. You need only traverse the array one time, use the email and mobile as unique key, the elements with the same unique key will only keep the last one. If you want the results use number index, use array_values() on the $result.

$result = [];
foreach($array as $v)
{
  $result[$v['customer_email'] . $v['customer_mobile']] = $v;
}
LF00
  • 27,015
  • 29
  • 156
  • 295
0

Here is my solution and is working fine.

    $name = array_column($array, 'name');
    $filteredKeys = array_unique($name);

    foreach (array_keys($filteredKeys) as $key => $value) {
    $filtered [] = $array[$value];
    }
      return  $filtered;
    }
DukesNuz
  • 87
  • 1
  • 9
  • There are two closing curly brackets, but only one opening; something seems to be missing here! – anpami Feb 27 '23 at 19:14
-1

My answer would be, that you should not do this in PHP at all. In your presented case the data should be checked/validated/filtered only on the database side. If there are duplicates, then you don't have to fetch data at all!

Run a query to just check for redundancies in db. Only if no redundancy fetch the data.

If there is a lot of data, then you'll spare a big data fetch and loop through data from the beginning.

Good luck.