PHP Array Diff

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 = mysqli_query($db, $command);
while($row = mysqli_fetch_assoc($result)) {
$id = $row['ID'];
$ID1[] = $id;
}

## Get second array
$command2 = "SELECT * FROM table2 ";
$result2 = mysqli_query($db, $command2);
$rows_count = mysqli_num_rows($result2);
if($rows_count >0) {
while($row = mysqli_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);