0

I have an associative multidimensional array:

Array
(
    [0] => Array
        (
            [customer_name] => John Dow
            [customer_email] => [email protected]
            [customer_mobile] => 1236547895
            [birth_date] => 12/1/1996
            [status] => Enable
        )

    [1] => Array
        (
            [customer_name] => Alex
            [customer_email] => [email protected]
            [customer_mobile] => 4563214785
            [birth_date] => 19/1/1996
            [status] => Enable
        )

    [2] => Array
        (
            [customer_name] => Arina
            [customer_email] => [email protected]
            [customer_mobile] => 963214785
            [birth_date] => 25/1/1996
            [status] => Enable
        )

    [3] => Array
        (
            [customer_name] => Atom
            [customer_email] => [email protected]
            [customer_mobile] => 5214789632
            [birth_date] => 12/1/1998
            [status] => Enable
        )

    [4] => Array
        (
            [customer_name] => Jennifer
            [customer_email] => [email protected]
            [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 :)

4
  • post the desired result Commented Jun 17, 2017 at 12:56
  • i don't want any result . i just want to check if any customer have duplicate contact number as well as email address. it return as flag - true means this array have contains redundancy Commented Jun 17, 2017 at 12:59
  • you wrote it return as flag - one flag for all items OR for each item separately? Commented Jun 17, 2017 at 13:03
  • on flag for all item - if 1 contact number repeated 2 or more times as well as email. entire array will be discarded Commented Jun 17, 2017 at 13:13

6 Answers 6

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';
}
Sign up to request clarification or add additional context in comments.

Comments

1

Simple solution is:

<?php

$data = [
  [
    'name' => 'name 1',
    'phone' => '12341234',
    'email' => '[email protected]'
  ],
  [
    'name' => 'name 2',
    'phone' => '12341234',
    'email' => '[email protected]'
  ],
  [
    'name' => 'name 3',
    'phone' => '4322342',
    'email' => '[email protected]'
  ],
  [
    'name' => 'name 4',
    'phone' => '1234123423',
    'email' => '[email protected]'
  ],
  [
    'name' => 'name 5',
    'phone' => '12341266634',
    'email' => '[email protected]'
  ],
];

$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) "[email protected]"
  }
  [2] =>
  array(3) {
    'name' =>
    string(6) "name 3"
    'phone' =>
    string(7) "4322342"
    'email' =>
    string(13) "[email protected]"
  }
  [4] =>
  array(3) {
    'name' =>
    string(6) "name 5"
    'phone' =>
    string(11) "12341266634"
    'email' =>
    string(18) "[email protected]"
  }
}

this is just example.

Comments

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;
}

Comments

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.

3 Comments

there is no any value in array..!
if 1 contact number repeated 2 or more times as well as email. entire array will be discarded.. how can i achienve this?
@DevendraSingh - again I make correction due to your new requirements - now you have boolean answer in $discard variable as well as 'flags' in redundant persons
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;
    }

1 Comment

There are two closing curly brackets, but only one opening; something seems to be missing here!
-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.

Comments

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.