【发布时间】:2017-01-16 08:17:02
【问题描述】:
我正在尝试从数组中删除重复的条目,然后将其显示在表格中。
这是我使用的数据格式示例:
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 方法可以正确地做到这一点,并在显示之前减少数组。
【问题讨论】:
-
显示一个包含实际 HTML 的 sn-p 怎么样?
-
HTML sn-p 添加
-
我会更上一层,如果您的数据来自数据库查询,也许您可以修改查询以不返回重复条目,再上一层,也许您可以保持完整性会阻止您首先插入这些重复项的约束。
-
数据来自 CSV,所以我无法在更高级别进行编辑:/
-
添加了一个有效的 HTML sn-p
标签: php jquery duplicates