Bit of a setup here to explain my issue, but in theory, all I need to do is order a multi-dimensional array before it outputs. The issue I'm having is that it's not grouping all the As, Bs and Cs etc together. I think it needs to sort all the names, before it adds the first letter...
<?php query_posts ( array (
'post_type' => 'programme',
'category_name' => 'archive',
'order' => 'DESC' ) ); ?>
<div class="container_12">
<div class="prefix_1 grid_10 suffix_1">
<?php while ( have_posts() ) : the_post(); ?>
<?php
$groups = array();
if ( have_rows('artists') ) {
while ( have_rows('artists') ) {
the_row();
// vars
$first_name = get_sub_field('first_name');
$last_name = get_sub_field('last_name');
$first_letter = substr($last_name, 0, 1);
// add $first_letter holder to groups
if( !isset($groups[ $first_letter ]) ) {
$groups[ $first_letter ] = array();
}
// append artist to group
$groups[ $first_letter ][] = $first_name . ' ' . $last_name;
}
}
// ouput
if( !empty($groups) ): ?>
<?php foreach( $groups as $letter => $artists ) : ?>
<h3><?php echo $letter; ?></h3>
<?php foreach( $artists as $artist ): ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</div>
<div class="clear"></div>
<?php wp_reset_postdata(); ?>
This currently outputs as:

And if you see in the image below, where I have just print_r( $groups ) it needs to add all the arrays into one big array, then sort it.

So, ultimately, I'd like to end up with something like:
A
Joe Allan
Frank Aztec
B
Jane Bank
C
Mike Crichton
Mandy Curtz
UPDATE
This is what I have no, and need to sort the artists, alphabetically, by their last name.
<?php if( !empty($groups) ) : ?>
<?php ksort($groups); ?>
<?php foreach ( $groups as $letter => $artists ) : ?>
<div>
<h3><?php echo $letter; ?></h3>
<?php asort($artists); ?>
<?php foreach( $artists as $artist ) : ?>
<p><?php echo $artist; ?></p>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
This gives me, using B as an example:
B
Berendes, Eva
Bloor, Simon
Bloor, Tom
Burt, Theo
Barnes, Kristian
Bajo, Elena
$groupsarray in each while loop iteration, and you are making the output inside your loop as well – if you actually want to list the artists of all posts in one list in the end, then of course you have to initialize the array outside of the loop, and also make the output after the loop.