If you would like to realize update rows by only allowing delete and insert rows on DB, set operation might be helpful.
For PHP, using array_diff function is really useful for realizing the operation.
For PHP, using array_diff function is really useful for realizing the operation.
// we would like to insert '4', '5' and delete '2' in this example.
// how to do this?
$original_ids = array('1', '2', '3');
$new_ids      = array('1',      '3', '4', '5');
// one solution
// 1) the key point is calculating subtract set.
// 2) array_values is used only for re-numbering index. e.g. all index will be 0 origin sequence .
// the result will be array(1){ [0] => '4', [0] => '5' }
$ids_insert = array_values(array_diff($new_ids, $original_ids));
// the result will be array(1){ [0] => '2' }
$ids_delete = array_values(array_diff($new_ids, $original_ids));
コメント