Get max value from two arrays inside two ACF nested repeaters

Viewed 21

I want to build a function that will echo/display the highest (max) value from two different arrays (repeater fields). The difficulty of the task lies into a specific construction of my repeaters / fields which looks as follows:

+Main repeater (called 'sales')

++sub_field('all_regions_percentage_off')

++Nested repeater (called 'different_regions')

+++ sub_field('percentage_off')

All of the subfields are text one, in which a user inputs numbers. So at the end I would like to display a single value which would the highest among those two fields/arrays. I have the following code:

<?php
    
    $groupA = array();
    $groupB = array();
    
    if( have_rows('sales') ):
        while( have_rows('sales') ) : the_row();
            
        
                $groupA[] = get_sub_field('all_regions_percentage_off');

                if( have_rows('different_regions') ):
                    while( have_rows('different_regions') ) : the_row();

                    $groupB = array_push($groupA, get_sub_field('percentage_off'));
                                    
                    endwhile;
                endif;

                echo '<pre>'.print_r($groupB).'</pre>';
        

        endwhile;
    else : echo 'Nothing here, sorry';
    endif;

?>

But it doesn't work and I have no idea why. Plus if the repeater has more than 1 row, the loop displays multiple outputs. Obviously it might be a thing of adding an counter like:

$i = 0; // before first while of the main repeater

if(!$i++) { code here } // as a wrapping element for the first subfield and the entire nested repeater 

But again the main issue is in a different place = showing a single value that will be a MAX from both fields (all_regions_percentage_off and percentage_off).

Thanks for any tips and suggestions.

0 Answers
Related