2017-06-17 12 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

私は結果を望んでいません。私はちょうど顧客が電子メールアドレスと同様に重複連絡先番号を持っているかどうか確認したい。これはフラグとして返されます - true は、この配列に冗長性が含まれていることを意味します –

+0

*フラグとして返す* - すべてのアイテムに対して1つのフラグまたは各アイテムに対して別々にフラグを設定しますか? – 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_emailまたはredundant_mobileをtrueに設定しました。変数$discard=trueは、配列が冗長であることを示します。

+0

配列には何も値はありません..! –

+0

1件の連絡先が2回以上繰り返されている場合はメールと同様です。全体の配列は破棄されます..どのように私はこれをachienveすることができますか? –

+0

@DevendraSingh - あなたの新しい要件のために訂正を行います。今や、 '$ discard'変数と冗長な人の' flags ' –

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とお試しください。一度だけ配列をたどるだけで、電子メールとモバイルを一意のキーとして使用する必要があります。同じユニークキーを持つ要素は、最後のものを保持します。結果の数値インデックスを使用する場合は、$resultarray_values()を使用してください。

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

私の答えは、PHPでこれを行うべきではないということです。提示されたケースでは、データはデータベース側でのみチェック/検証/フィルタリングする必要があります。重複がある場合、データをまったくフェッチする必要はありません。

dbで冗長性をチェックするためのクエリを実行します。冗長性がない場合にのみデータをフェッチします。

大量のデータがある場合は、大量のデータフェッチを行い、最初からデータをループします。

幸運。

関連する問題