Never mind! I fixed it by using the full field keys instead of slugs. Here’s the working code:
if ( $items->have_posts() ) {
$tax_var = get_terms('varieties_grown', 'hide_empty=0');
$tax_var_array = array();
foreach ( $tax_var as $tax ) {
$tax_var_array[] = array('term_id' => $tax->term_id, 'name' => $tax->name );
}
while( $items->have_posts() ) {
$items->the_post();
$post_id = get_the_ID();
if( have_rows('field_57d8b8c860d41')):
while ( have_rows( 'field_57d8b8c860d41' ) ) : the_row();
$variety = get_sub_field( 'field_57d8b8dc60d42' );
$key = array_search( $variety, array_column( $tax_var_array, 'name' ) );
if ( $key ) {
$term_id = $tax_var_array[$key]['term_id'];
update_sub_field('field_5894f9fbbad27', $term_id );
}
endwhile;
endif;
}
}