You need to break
both foreach
loops not just one, so add break 2;
after the echo
.
$mycats = array(106, 107);
foreach (get_the_category() as $childcat) {
foreach ($mycats as $mycat) {
if (cat_is_ancestor_of($mycat, $childcat)) {
echo '<a href="'.get_category_link($childcat->cat_ID).'">' . $childcat->cat_name . '</a>';
break 2;
}
}
}