Support

Account

Home Forums Add-ons Repeater Field Combine 2 repeater rows with same values Reply To: Combine 2 repeater rows with same values

  • The arrays cannot be combined while they are being fetched and printed. Store them in another array instead and then process them all at the same time.
    I did not run this code, so watch out for missing punctuation and such:

    <?php
    $this_array  = get_field( 'this_array' );
    $that_array  = get_field( 'that_array' );
    $final_array = []; // An array of all rows.
    
    // Add all rows of $this_array to the array of all rows.
    if ( have_rows( 'this_array' ) ) :
    	while ( have_rows( 'this_array' ) ) : the_row();
    		$final_array[] = [
    			'arg1' => get_sub_field( 'arg1' ) ,
    			'arg2' => get_sub_field( 'arg2' ) ,
    		];
    	endwhile;
    endif;
    
    // Add all rows of $that_array to the array of all rows.
    if ( have_rows( 'that_array' ) ) :
    	while ( have_rows( 'that_array' ) ) : the_row();
    		$final_array[] = [
    			'arg1' => get_sub_field( 'arg1' ) ,
    			'arg2' => get_sub_field( 'arg2' ) ,
    		];
    	endwhile;
    endif;
    
    // Sort the $final_array here.
    // See: https://www.php.net/manual/en/array.sorting.php
    
    // Print each row.
    foreach ( $final_array AS $row ) {
    	echo '<li>' . $row['arg1'] . '<span>' . $row['arg2'] . '</span></li>';
    }