ACF bootstrap手风琴最后一个总是打开

I'm using Advanced Custom Fields (ACF) together with Bootstrap 4. The accordion is in repeater fields. I want have an accordion with the last item always open. I know how to do this with the first item always open. See code below. Because I do not know how many items there are gonna be (because this can be different for each CPT), I can't use $i==1, or $i==2, etc.

Please see code below for the first item always open, but need to have the last item always open.

<?php if( have_rows('faq') ): ?>
<div id="accordion" role="tablist">
  <?php $i=1; while ( have_rows('faq') ) : the_row(); ?>
    <div class="card">
        <div class="card-header" role="tab" id="heading-<?php echo $i; ?>">
          <h5 class="mb-0">
            <a data-toggle="collapse" href="#collapse-<?php echo $i; ?>" aria-expanded="true" aria-controls="collapseOne">
             <?php the_sub_field('vraag'); ?>
            </a>
          </h5>
        </div>
        <div id="collapse-<?php echo $i; ?>" class="collapse <?php if ($i==1) { echo 'show'; } ?>" role="tabpanel" data-parent="#accordion" aria-labelledby="heading-<?php echo $i; ?>">
          <div class="card-body">
            <?php the_sub_field('antwoord'); ?>
          </div>
        </div>
    </div>
  <?php $i++; endwhile; ?>
</div>

Hope someone points m,e to the right direction how to manage this.

Thanks in advanced.

You could try doing your ACF loop and assigning all the values to a php array. Then you can count the array so you will have the index of the last item. And you can also use this array for generating your accordion (so that you don't run the query twice).

You can also use count on ACF (but I haven;t tried it so I am not quite sure): $count = count(get_field('faq'));

# here is an example of your code with count.

<?php if( have_rows('faq') ): ?>
<?php $count = count(get_field('faq'));  ?>
<div id="accordion" role="tablist">
  <?php $i=1; while ( have_rows('faq') ) : the_row(); ?>
    <div class="card">
        <div class="card-header" role="tab" id="heading-<?php echo $i; ?>">
          <h5 class="mb-0">
            <a data-toggle="collapse" href="#collapse-<?php echo $i; ?>" aria-expanded="true" aria-controls="collapseOne">
             <?php the_sub_field('vraag'); ?>
            </a>
          </h5>
        </div>
        <div id="collapse-<?php echo $i; ?>" class="collapse <?php if ($i == $count) { echo 'show'; } ?>" role="tabpanel" data-parent="#accordion" aria-labelledby="heading-<?php echo $i; ?>">
          <div class="card-body">
            <?php the_sub_field('antwoord'); ?>
          </div>
        </div>
    </div>
  <?php $i++; endwhile; ?>
</div>