根据两个数据

时间:2016-09-08 11:55:06

标签: php jquery duplicates

我正在尝试从数组中删除重复的条目,然后将其显示在表格中。

以下是我使用的数据格式示例:

array (size=4)
  0 => 
    array (size=5)
      'Statut' => string 'REFUSED' (length=5)
      'Username' => string 'name' (length=4)
      'Nom' => string 'firstname' (length=9)
      'Email' => string 'mail@gmail.com' (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 'mail@gmail.com' (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 'mail@gmail.com' (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 'othermail@gmail.com' (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">mail@gmail.com</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">mail@gmail.fr</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">mail@gmail.com</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">othermail@gmail.com</td>
                        <td class="regularisation">N</td>
        </tr>
    </tbody>
  </table>

JS正在做这项工作:

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

在JS之后,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">mail@gmail.com</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">othermail@gmail.com</td>
                            <td class="regularisation">N</td>
            </tr>
        </tbody>
      </table>

问题是仅使用邮件是不够的:

如果我有两个具有相同邮件但具有不同法规的条目,我应该保留其中一个。

所以上面的例子将减少为:

array (size=3)
      0 => 
        array (size=5)
          'Statut' => string 'REFUSED' (length=5)
          'Username' => string 'name' (length=4)
          'Nom' => string 'firstname' (length=9)
          'Email' => string 'mail@gmail.com' (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 'mail@gmail.com' (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 'othermail@gmail.com' (length=27)
          'Regularisation' => string 'N' (length=1)

我很确定我可以通过DOM导航来完成它,但我想知道是否有任何PHP方法可以正确地执行它并在显示之前减少数组。

2 个答案:

答案 0 :(得分:1)

使用php unset函数从数组中删除,如下所示:

$dataArray = array (
              0 => 
                array (
                  'Statut' => 'REFUSED',
                  'Username' =>  'name',
                  'Nom' =>  'firstname',
                  'Email' =>  'mail@gmail.com' ,
                  'Regularisation' =>  'N' ,
                 ),
              1 => 
                array (
                  'Statut' =>  'REFUSED' ,
                  'Username' =>  'name' ,
                  'Nom' =>  'firstname' ,
                  'Email' =>  'mail@gmail.com' ,
                  'Regularisation' =>  'N',
                 ),
              2 => 
                array (
                  'Statut' =>  'VALID', 
                  'Username' =>  'name', 
                  'Nom' =>  'firstname',
                  'Email' =>  'mail@gmail.com', 
                  'Regularisation' =>  'N' ,
                 ),
              3 => 
                array (
                  'Statut' =>  'VALID' ,
                  'Username' =>  'user2' ,
                  'Nom' =>  'second_nae' ,
                  'Email' =>  'othermail@gmail.com', 
                  '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);

这会给你:

Array
(
    [1] => Array
        (
            [Statut] => REFUSED
            [Username] => name
            [Nom] => firstname
            [Email] => mail@gmail.com
            [Regularisation] => N
        )

    [2] => Array
        (
            [Statut] => VALID
            [Username] => name
            [Nom] => firstname
            [Email] => mail@gmail.com
            [Regularisation] => N
        )

    [3] => Array
        (
            [Statut] => VALID
            [Username] => user2
            [Nom] => second_nae
            [Email] => othermail@gmail.com
            [Regularisation] => N
        )

)

答案 1 :(得分: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
}