So I put together this address format array and it works really well:
$address_street = get_field( 'address_street' );
$address_city = get_field( 'address_city' );
$address_postcode = get_field( 'address_postcode' );
$address_state = get_field( 'address_state' );
$terms = get_the_terms($post->id, 'address_country');
foreach ( $terms as $term ) { }
?>
<div id="address-widget">
<p><strong>Where to find us?</strong></p>
<?php $arr = array($address_street, $address_city, $address_postcode, $address_state, $term->name); ?>
<p class="address-format">
<?php echo implode(", ", $arr); ?>
</p>
</div>
and it outputs all the details of the address separated by coma, just like I wanted.
The problem now is that $address_street, $address_postcode and $address_state are not compulsory fields, so if they are not filled in, that they would be left blank without the extra coma.
So I can find how to input the array with items that are conditional here: How can I add a condition inside a php array?, but how do I add the condition inside the array, leave the blanks out and have the filled out elements separated by coma in the same time?
So if only the compulsory fields (city, country) are filled in, I would get output like this: City, Country
<?php }?>should be<?php } ?>, you are using$termafter closingforeachwhich makes no sense and an open and close curly braces$arr = array($address_street, $address_city, $address_postcode, $address_state, $term->name); { //something }without anything written or condition....