0

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

2
  • Your code has several errors... Like <?php }?> should be <?php } ?> , you are using $term after closing foreach which 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.... Commented Jun 13, 2017 at 12:18
  • Thanks Praveen, fixed now .. Commented Jun 13, 2017 at 12:24

3 Answers 3

3

If I got you right the solution is probably simpler than you think. What you want is to print all the array values (if they are not empty) in a comma separated string. What you can do to achieve this is to simply remove empty elements from the array when printing:

<?php echo implode(", ", array_filter($arr)); ?>

This will remove empty elements from the array before imploding it into a comma separated string.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't think it will be that simple, but it works absolutely great!! Many thanks!!
1

if(get_field( 'address_street' ) != ''){

$address_street = get_field( 'address_street' ); }

if(get_field( 'address_city' ) != ''){

$address_city = get_field( 'address_city' ); }

if(get_field( 'address_postcode' ) != ''){

$address_postcode = get_field( 'address_postcode' ); }

if(get_field( 'address_state' ) != ''){

$address_state = get_field( 'address_state' ); }

Comments

0

You can try to remove the empty values from the array before the implode using:

$arr = array_filter($arr);

See more about the array_filter() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.