Support

Account

Home Forums Add-ons Repeater Field Repeater sub field display by group based on value Reply To: Repeater sub field display by group based on value

  • I was trying to do the same thing a while ago. Couldn’t figure out how to do it natively in ACF so I just made a normal PHP array, sorted it, then looped through it.

    Full credit to the following (my solution is basically these stuck together):

    http://stackoverflow.com/questions/5498718/how-to-sort-associative-array-using-sub-field-of-contained-associative-arrays-in
    https://www.advancedcustomfields.com/resources/repeater/

    
    <?php
    
    // check if the repeater field has rows of data
    if( have_rows('person_repeater') ):
    
        $personArray = get_field('person_repeater');
    
        function callback( $a, $b ) {
    
            if ( $a['person_group'] > $b['person_group'] ) {
    
                return 1;
    
            } else if ( $a['person_group'] < $b['person_group'] ) {
    
                return -1;
    
            }
    
            return 0;
    
        }
    
        uasort( $personArray, 'callback' );
    
        foreach ($personArray as $person) {
    
            ?>
    
            Group name: <?php echo $person['person_group']; ?>, Person Name: <?php echo $person['person_name']; ?><br>
    
        <?php
    
        }
    
    else :
    
        // no rows found
    
    endif;
    
    ?>