Logic of my function was all wrong, should have sorted the result in $trans_array
before the foreach loop.
Here is the updated code that worked, if anyone needs it:
function rkm_translation_update() {
global $wpdb;
$trans_row = $wpdb->get_row("SELECT * FROM id_item_lid", OBJECT, 0);
$id = $trans_row->id;
$item = $trans_row->item_id;
$lid = $trans_row->lid;
$trans_array = $wpdb->get_results("SELECT * FROM id_item_lid ORDER BY item_id");
foreach ($trans_array as $trans) {
$id_new = $trans->id;
$item_new = $trans->item_id;
$lid_new = $trans->lid;
if ($item === $item_new) {
$wpdb->update('wp_icl_translations', array('trid' => $id, 'source_language_code' => $lid), array('element_id' => $id_new, 'element_type' => 'post_post'));
} else {
$id = $trans->id;
$item = $trans->item_id;
$lid = $trans->lid;
$wpdb->update('wp_icl_translations', array('trid' => $id, 'source_language_code' => $lid), array('element_id' => $id, 'element_type' => 'post_post'));
}
}
}
This is a bit of a dirty solution, when debugging is true, there are some warnings in the admin related to original post language, if it is missing, probably could be refined a bit more, but I couldn’t be bothered.