2017-06-17 163 views
-1

我有一個關聯多維數組:如何檢測多維關聯數組中的重複值?

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

現在我想從對方customer_mobilecustomer_email檢查相似值,以減少冗餘。聯繫電話和電子郵件地址必須是非冗餘的。

那麼請指導我,我該如何實現這一目標?謝謝:)

+0

發佈預期結果 – RomanPerekhrest

+0

我不想要任何結果。我只是想檢查是否有客戶有重複的聯繫電話以及電子郵件地址。它返回爲標誌 - 真 意味着這個數組包含冗餘 –

+0

你寫*它作爲標誌返回* - 一個標誌爲所有項目或分別爲每個項目? – RomanPerekhrest

回答

0

簡單的解決方案是:

<?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); 

和結果,你會得到:

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]" 
    } 
} 

這僅僅是例子。

0

您可以用這種方式(我生成頭代碼,以便它可以有錯誤 - 但思路應該是清楚的)這樣做(我假定你的數組名是$人):

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

因此,每個使用移動設備或電子郵件的人都會將redundant_emailredundant_mobile設置爲true。變量$discard=true表示該數組是冗餘的。

+0

陣列中沒有任何值..! –

+0

如果1個聯繫人重複2次或更多次以及電子郵件。整個陣列將被丟棄..我如何achienve這? –

+0

@DevendraSingh - 由於您的新要求,我再次進行修正 - 現在您在'$ discard'變量中具有布爾型回答以及在冗餘人員中使用「標誌」 –

0

既然你不需要知道哪些,但只有如果,你可以使用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

if($cm != array_unique($cm) && $ce != array_unique($ce)){ 
    echo 'There are duplicates in both customer_mobile and customer_email'; 
} 
0

foreach試試這個。您只需要遍歷數組一次,使用電子郵件和移動設備作爲唯一鍵,具有相同唯一鍵的元素將只保留最後一個。如果您想要結果使用數字索引,請在$result上使用array_values()

$result = []; 
foreach($array as $v) 
{ 
    $result[$v['customer_email'] . $v['customer_mobile']] = $v; 
} 
0

我的答案是,你不應該在PHP中這樣做。在你提到的情況下,數據只應在數據庫端進行檢查/驗證/過濾。如果有重複,那麼你根本不需要獲取數據!

運行一個查詢來檢查數據庫中的冗餘。只有在沒有冗餘獲取數據的情況下。

如果有大量的數據,那麼您將從一開始就節省大量的數據提取和循環數據。

祝你好運。