|
May 17
2012
|
PHP Array DiffPosted by bohemia in Untagged |
|
PHP Array Diff
With the array_diff() function, you can compare 2 arrays and get the difference between the two of them. The code below takes ids from one table and compares it to the ids of another table.## Get first array
$command = "SELECT * FROM table1 ";
$result = mysql_query($command, $db);
while($row = mysql_fetch_assoc($result)) {
$id = $row['ID'];
$ID1[] = $id;
}
## Get second array
$command2 = "SELECT * FROM table2 ";
$result2 = mysql_query($command2, $db);
$rows_count = mysql_num_rows($result2);
if($rows_count >0) {
while($row = mysql_fetch_assoc($result2)) {
$id = $row['id'];
## Make an array of all ids
$id2[] = $id;
}
}
else{
$id2[]=0;
}
## CCompare teh difference of the ids in the 2 tables
$result_diff = array_diff($1id, $id2);
$command = "SELECT * FROM table1 ";
$result = mysql_query($command, $db);
while($row = mysql_fetch_assoc($result)) {
$id = $row['ID'];
$ID1[] = $id;
}
## Get second array
$command2 = "SELECT * FROM table2 ";
$result2 = mysql_query($command2, $db);
$rows_count = mysql_num_rows($result2);
if($rows_count >0) {
while($row = mysql_fetch_assoc($result2)) {
$id = $row['id'];
## Make an array of all ids
$id2[] = $id;
}
}
else{
$id2[]=0;
}
## CCompare teh difference of the ids in the 2 tables
$result_diff = array_diff($1id, $id2);




