如何简化这些步骤?php/sql 代码旨在从一个表中删除另一张表中不存在的party_id:
// party_id from one table
$array1 = array();
$db->setQuery("SELECT DISTINCT(record) FROM table1");
$contents = $db->loadObjectList();
foreach( $contents As $content ){
$row = $content->record;
$array1[] = $row;
}
// get party_id from different table
$array2 = array();
$db->setQuery("SELECT DISTINCT(party_id) FROM table2 WHERE user_id = '$id'");
$contents = $db->loadObjectList();
foreach ( $contents As $content ) {
$row = $content->party_id;
$array2[] = $row;
}
// Compare, then delete records existing in array2, but not in array1
$result = array_diff($array2, $array1);
foreach ( $result As $rslt ) {
$db->setQuery("DELETE FROM table2 WHERE party_id = '$rslt' AND user_id = '$id'");
$db->query();
}