the_field
is used to output, get_field
is used to return.
So instead of
$category1 = the_field('category1');
It should be
$category1 = get_field('category1');
the_field
is used to output, get_field
is used to return.
So instead of
$category1 = the_field('category1');
It should be
$category1 = get_field('category1');