Nirnae Nirnae - 2 months ago 10
PHP Question

Remove a row of table based on two data

I'm trying to delete duplicated entry from an array to then display it in a table.

Here is an example of the data format I use :

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)


Right now I manage to delete duplicate with some Jquery based on if I already saw the email somewhere.

Here is the HTML part :

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


And JS doing the work :

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


After JS, the HTML will look like this :

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


Problem is using only the mail isn't enough :

If I have two entries with the same mail but with different statut I should keep one of both.

So the example above would be reduce to :

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)


I'm pretty sure I could do it throught DOM navigation but I would like to know if there was any PHP way to do it properly and reduce the array even before displaying it.

Answer

Use php unset function to remove from array, like below:

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

This will give you :

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
        )

)
Comments