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 :)
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.