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>';
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.