2016-09-08 29 views
0

重複したエントリを配列から削除してテーブルに表示しようとしています。ここで2つのデータに基づいてテーブルの行を削除します

は、私が使用するデータフォーマットの例である:

array (size=4) 
    0 => 
    array (size=5) 
     'Statut' => string 'REFUSED' (length=5) 
     'Username' => string 'name' (length=4) 
     'Nom' => string 'firstname' (length=9) 
     'Email' => string '[email protected]' (length=14) 
     'Regularisation' => string 'N' (length=1) 
    1 => 
    array (size=5) 
     'Statut' => string 'REFUSED' (length=5) 
     'Username' => string 'name' (length=4) 
     'Nom' => string 'firstname' (length=9) 
     'Email' => string '[email protected]' (length=14) 
     'Regularisation' => string 'N' (length=1) 
    2 => 
    array (size=5) 
     'Statut' => string 'VALID' (length=5) 
     'Username' => string 'name' (length=12) 
     'Nom' => string 'firstname' (length=19) 
     'Email' => string '[email protected]' (length=14) 
     'Regularisation' => string 'N' (length=1) 
    3 => 
    array (size=5) 
     'Statut' => string 'VALID' (length=5) 
     'Username' => string 'user2' (length=10) 
     'Nom' => string 'second_nae' (length=8) 
     'Email' => string '[email protected]' (length=27) 
     'Regularisation' => string 'N' (length=1) 

は、今私は、私はすでにどこかに電子メールを見た場合に基づいて、いくつかのjQueryを使って、重複を削除するために管理します。ここで

はHTMLの一部です:

<table id="myTable" class="tablesorter"> 
    <thead> 
    <tr> 
    <th id="Statut">Statut</th> 
    <th id="Username">Username</th> 
    <th id="Nom">Nom</th> 
    <th id="Email">Email</th> 
    <th id="Regularisation">Regularisation</th>   
    </tr> 
    </thead> 
     <tbody> 
     <tr class="row"> 
         <td class="statut">REFUSED</td> 
         <td class="username">name</td> 
         <td class="nom">firstname</td> 
         <td class="mail">[email protected]</td> 
         <td class="regularisation">N</td> 
     </tr><tr class="row"> 
         <td class="statut">REFUSED</td> 
         <td class="username">name</td> 
         <td class="nom">firstname</td> 
         <td class="mail">[email protected]</td> 
         <td class="regularisation">N</td> 
     </tr><tr class="row"> 
         <td class="statut">VALID</td> 
         <td class="username">name</td> 
         <td class="nom">firstname</td> 
         <td class="mail">[email protected]</td> 
         <td class="regularisation">N</td> 
     </tr><tr class="row"> 
         <td class="statut">VALID</td> 
         <td class="username">user2</td> 
         <td class="nom">second_nae</td> 
         <td class="mail">[email protected]</td> 
         <td class="regularisation">N</td> 
     </tr> 
    </tbody> 
    </table> 

そしてJS仕事をしている:JS後

var seen = {}; 
$('.mail').each(function() { 
    var txt = $(this).text(); 
    if (seen[txt]) 
     $(this).parent().remove(); 
    else 
     seen[txt] = true; 
}); 

、HTMLは次のようになります。

<table id="myTable" class="tablesorter"> 
     <thead> 
     <tr> 
     <th id="Statut">Statut</th> 
     <th id="Username">Username</th> 
     <th id="Nom">Nom</th> 
     <th id="Email">Email</th> 
     <th id="Regularisation">Regularisation</th>   
     </tr> 
     </thead> 
      <tbody> 
      <tr class="row"> 
          <td class="statut">REFUSED</td> 
          <td class="username">name</td> 
          <td class="nom">firstname</td> 
          <td class="mail">[email protected]</td> 
          <td class="regularisation">N</td> 
      </tr><tr class="row"> 
          <td class="statut">VALID</td> 
          <td class="username">user2</td> 
          <td class="nom">second_nae</td> 
          <td class="mail">[email protected]</td> 
          <td class="regularisation">N</td> 
      </tr> 
     </tbody> 
     </table> 

問題を使用していますメールのみでは不十分です:

私は同じメールで2つのエントリーを持っていますが、異なるスタウトを持っている場合は、どちらか一方を保つ必要があります。

したがって、上記の例では、に削減されるだろう:

array (size=3) 
     0 => 
     array (size=5) 
      'Statut' => string 'REFUSED' (length=5) 
      'Username' => string 'name' (length=4) 
      'Nom' => string 'firstname' (length=9) 
      'Email' => string '[email protected]' (length=14) 
      'Regularisation' => string 'N' (length=1) 
     1 => 
     array (size=5) 
      'Statut' => string 'VALID' (length=5) 
      'Username' => string 'name' (length=12) 
      'Nom' => string 'firstname' (length=19) 
      'Email' => string '[email protected]' (length=14) 
      'Regularisation' => string 'N' (length=1) 
     2 => 
     array (size=5) 
      'Statut' => string 'VALID' (length=5) 
      'Username' => string 'user2' (length=10) 
      'Nom' => string 'second_nae' (length=8) 
      'Email' => string '[email protected]' (length=27) 
      'Regularisation' => string 'N' (length=1) 

私はそれがDOMナビゲーションをthroughtが、私はそれを正しく行うと、減らすために任意のPHPの方法があったかどうかを知りたいと思いませんでしたかなり確信しています配列を表示する前に。

+0

を? – mplungjan

+0

HTMLスニペットが追加されました – Nirnae

+0

データがデータベースクエリから来ている場合は、レベルを1つ上げてください。重複したエントリを返さないようにクエリを変更し、それよりも高いレベルにすることもできます。それらの重複を最初から挿入しないようにします。 – apokryfos

答えて

1

以下のように、配列から削除するPHPを使用解除機能:

$dataArray = array (
       0 => 
       array (
        'Statut' => 'REFUSED', 
        'Username' => 'name', 
        'Nom' => 'firstname', 
        'Email' => '[email protected]' , 
        'Regularisation' => 'N' , 
       ), 
       1 => 
       array (
        'Statut' => 'REFUSED' , 
        'Username' => 'name' , 
        'Nom' => 'firstname' , 
        'Email' => '[email protected]' , 
        'Regularisation' => 'N', 
       ), 
       2 => 
       array (
        'Statut' => 'VALID', 
        'Username' => 'name', 
        'Nom' => 'firstname', 
        'Email' => '[email protected]', 
        'Regularisation' => 'N' , 
       ), 
       3 => 
       array (
        'Statut' => 'VALID' , 
        'Username' => 'user2' , 
        'Nom' => 'second_nae' , 
        'Email' => '[email protected]', 
        'Regularisation' => 'N' , 
       ),  
      );  

foreach($dataArray as $key => $val) 
{ 
    foreach($dataArray as $k => $v) 
    { 
     if($key != $k && $val['Email'] == $v['Email'] && $val['Statut'] == $v['Statut']) //here you can add multiple keys 
     { 
      unset($dataArray[$key]); 
     } 
    } 
} 
echo "<pre>"; print_r($dataArray); 

これはあなたを与えるだろう:実際のHTMLが含まれているスニペットを示すについてどのように

Array 
(
    [1] => Array 
     (
      [Statut] => REFUSED 
      [Username] => name 
      [Nom] => firstname 
      [Email] => [email protected] 
      [Regularisation] => N 
     ) 

    [2] => Array 
     (
      [Statut] => VALID 
      [Username] => name 
      [Nom] => firstname 
      [Email] => [email protected] 
      [Regularisation] => N 
     ) 

    [3] => Array 
     (
      [Statut] => VALID 
      [Username] => user2 
      [Nom] => second_nae 
      [Email] => [email protected] 
      [Regularisation] => N 
     ) 

) 
+0

完璧に、ありがとう! – Nirnae

+0

偉大な:)あなたを助けてうれしい親愛なる@Nirnae –

0

PHPで確認してください。

$newarray = array(); // create the new array to store the email, status combo 

foreach($userData as $k=>$v) 
{ 
    $ele = $v['Email'].'::'.$v['Statut']; 

    if(in_array($ele,$newarray)) 
     unset($userData[$k]); // remove the element whose email and status existed already 

    array_push($v['Email'].'::'.$v['Statut']); // push every new email , status in the new array to check further 
} 
関連する問題