Reputation: 46
I have 2 php arrays :
First One :
<?php
while ($res_liste_risks_last_update_moins_un = mysql_fetch_array($sql_liste_risks_last_update_moins_un)) {
$myTAB_last_update_moins_un[$compteur_risk_moins_un] = array(
'MyRisk_level_xls' => $res_liste_risks_last_update_moins_un['MyRisk_level_xls'],
'MyRisk_SN_xls' => $res_liste_risks_last_update_moins_un['MyRisk_SN_xls'],
'MyRisk_content_xls' => $res_liste_risks_last_update_moins_un['MyRisk_content_xls']
);
$compteur_risk_moins_un++;
}
Second One :
<?php
while ($res_liste_risks_last_update = mysql_fetch_array($sql_liste_risks_last_update)) {
$myTAB_last_update[$compteur_risk] = array(
'MyRisk_level_xls' => $res_liste_risks_last_update['MyRisk_level_xls'],
'MyRisk_SN_xls' => $res_liste_risks_last_update['MyRisk_SN_xls'],
'MyRisk_content_xls' => $res_liste_risks_last_update['MyRisk_content_xls']
);
$compteur_risk++;
}
The structure of this 2 arrays is the same.
For information, I fill these 2 arrays with 2 mysql queries. The data are present on the same table.
i would like to get the difference between this 2 arrays :
The new line present on the second array (myTAB_last_update) (regarding the value present on first array $myTAB_last_update_moins_un)
The suppressed line (no more present) on the second array (myTAB_last_update) (regarding the value present on first array $myTAB_last_update_moins_un)
I tried several method like :
PHP : array_diff_assoc => This solution give me the good number of line suppressed but not the good value. And this solution not return the new line.
PHP : 2 nested foreach (sorry i'm not sure that nested is the good word in English)
MYSQL : create a mysql query with 2 SELECT and 1 IN
I never found any solution to my problem.
To be more clear, here a easiest explanation :
Array_1 :
0 value1 value2
1 value1 value2
2 value1 value2
3 value1 value2
4 value1 value2
5 value1 value2
Array_2 :
0 value1 value2
2 value1 value2
3 value1 value2
4 value1 value2
5 value1 value2
6 value1 value2
On the second array :
=> line 2 was suppressed
=> line 6 was added
i want to do that with my own arrays.
Have you got any ideas ?
Best regards,
Florent.
Upvotes: 1
Views: 68
Reputation: 46
So i found a solution for my problem. Perhaps it's not a conventionnal way but it works.
So you could find below an explanation : - Modify my 2 arrays with a concatenation
while($res_liste_risks_last_update_moins_un = mysql_fetch_array($sql_liste_risks_last_update_moins_un))
{
$chaine_update_moins_un = $res_liste_risks_last_update_moins_un['MyRisk_level_xls'] . '__' . $res_liste_risks_last_update_moins_un['MyRisk_SN_xls'] . '__' . $res_liste_risks_last_update_moins_un['MyRisk_content_xls'];
$myTAB_last_update_moins_un[$compteur_risk_moins_un] = $chaine_update_moins_un;
$compteur_risk_moins_un++;
}
while($res_liste_risks_last_update = mysql_fetch_array($sql_liste_risks_last_update))
{
$chaine_update = $res_liste_risks_last_update['MyRisk_level_xls'] . '__' . $res_liste_risks_last_update['MyRisk_SN_xls'] . '__' . $res_liste_risks_last_update['MyRisk_content_xls'];
$myTAB_last_update[$compteur_risk] = $chaine_update;
$compteur_risk++;
}
Then :
$added = array_diff($myTAB_last_update, $myTAB_last_update_moins_un);
echo '<pre>';
print_r($added);
echo '</pre>';
$deleted = array_diff($myTAB_last_update_moins_un, $myTAB_last_update);
echo '<pre>';
print_r($deleted);
echo '</pre>';
So now, i can print the lines added and the lines suppressed.
After that i will realize an explode and it will be good ;-)
Thank you for your help.
Best regards,
Florent.
Upvotes: 0
Reputation:
Ok it was a little idiot, I've forgot you was creating rows ID from 0 to X for the twice arrays (red number on your Array_2 example are not 023456 but 012345 like the first). So we can't use it to compare lines.
I test this code and it seem good:
$added = array_diff($tab2, $tab);
$deleted = array_diff($tab, $tab2);
foreach(array_keys($added) as $no_row){
echo "Line ".($no_row+1)." added<br>";
}
foreach(array_keys($deleted) as $no_row){
echo "Line ".($no_row+1)." deleted<br>";
}
It print the line deleted in array1 and added in array2. Problem will be if you have 2 different rows with same values ...
Upvotes: 1
Reputation:
What's about that ?
$added = array_diff_key($new, $old);
$deleted = array_diff_key($old, $new);
foreach(array_keys($added) as $no_row){
echo "Line ".$no_row." added";
}
foreach(array_keys($deleted) as $no_row){
echo "Line ".$no_row." deleted";
}
Waiting your feedback ;)
Upvotes: 2