Hello,
I have a question about 2 arrays i have to save checkboxes selected in a form to update options for a part of a plugin i'm working on in wordpress.
Probably something simple but i can't get to it.
The situation:
$array1 = ([0] => 1, [1] => 2, [2] => 3, [3] => 4);
$array2 = ([0] => 13, [1] => 5, [2] => 2, [3] => 8, [4] => 1);
$array1 comes from a form based on checkboxes. Can be 0 or 20 or whatever much are clicked.
$array2 comes from mysql based on the amount of records found. Length depends on records available.
The values are different depending on what options are clicked or which records are pulled but are ALWAYS numbers.
What needs to happen:
- Values 1 and 2 are in both arrays and can be left alone.
- Values 3 and 4 in $array1 are not in $array2 so they need to be added to mysql.
- Values 5, 8 and 13 from $array2 are not in $array1 so they need to be deleted from mysql.
My broken code:
I came up with this so far, but it lacks the delete option and the insert doesn't work either.
// $id is a supplied ID and works as intended
$array2 = $wpdb->get_results("SELECT `value` FROM `table` WHERE `column2` = '$id';");
foreach($array2 as $result) {
$i = 0;
while($i <= count($array1)) {
if(!in_array($result->value, $array1)) {
$wpdb->query("INSERT INTO `table` (`column1`, `column2`, `column3`) VALUES ('$array1[$i]', '$id', '0';");
}
$i++;
}
}
Any ideas or suggestions would be very welcome!
Thanks in advance!
Arnan