Home › Forums › Add-ons › Repeater Field › Combine 2 repeater rows with same values
I would like to combine 2 rows so that I can merge them alphabetically into a list. The first row takes a select value and a text price and the second row takes a text value and a text price.
I tried using array_merge_recursive but it doesn’t result into a row so it wont print in my if/while row loop. (its not the loop as the loop works with the un-merged arrays.
Without you providing any code samples I can only guess what is really happening on your site, but the following might be applicable:
<?php
$i = 0;
while( ... ) {
if ( $i == 0 ) {
// Do an odd row...
$i++;
} else {
// Do an even row...
$i--;
}
}
unset( $i );
Thanks, what I mean is to combine two arrays (captured from two different acf repeater rows), so that they combine and can be sorted alphabetically.
Each repeater has these fields
$this_array = repeater fields $arg1 and $arg2
$that_array = repeater fields $arg1 and $arg2
$result = array_merge_recursive($ar1, $ar2);
sort($result);
This sample does note combine them and display them alphabetically:
<?php
$this_array = get_field('this_array');
$that_array = get_field('that_array');
?>
<ul class="panel">
<?php if( have_rows('this_array') ): ?>
<?php while ( have_rows('this_array') ) : the_row();
$arg1 = get_sub_field('arg1');
$arg2 = get_sub_field('arg2');
?>
<li><?php the_sub_field('arg1') ?><span>
<?php the_sub_field('arg2') ?></span></li>
<?php endwhile; ?>
<?php else : // no rows found
endif; ?>
<?php if( have_rows('that_array') ): ?>
<?php while ( have_rows('that_array') ) : the_row();
$arg1 = get_sub_field('arg1');
$arg2 = get_sub_field('arg2');
?>
<li><?php the_sub_field('arg1') ?><span>
<?php the_sub_field('arg2') ?>
</span></li>
<?php endwhile; ?>
<?php else : // no rows found
endif; ?>
</ul>
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>';
}
Thank you, I managed to get that but they are still not merging alphabetically.
Here is my code:
`<!– ********************************** PIGEONS AND DOVES –>
<?php if( ($pigeons_and_doves || $other_pigeons_and_doves) ): //<!– THIS WILL NOT HIDE when empty –>
// Add all rows of $this_array to the array of all rows.
if ( have_rows( ‘pigeons_and_doves’ ) ) :
while ( have_rows( ‘pigeons_and_doves’ ) ) : the_row();
$final_array[] = [
‘bird_name’ => get_sub_field( ‘pigeon_and_dove_species’ ) ,
‘price’ => get_sub_field( ‘bird_price’ ) ,
];
endwhile;
endif;
// Add all rows of $that_array to the array of all rows.
if ( have_rows( ‘other_pigeons_and_doves’ ) ) :
while ( have_rows( ‘other_pigeons_and_doves’ ) ) : the_row();
$final_array[] = [
‘bird_name’ => get_sub_field( ‘other_pigeon_and_dove_species’ ) ,
‘price’ => get_sub_field( ‘other_bird_price’ ) ,
];
endwhile;
endif;
sort($final_array);
?>
<div class=”animal-listing-col”>
<table>
<tr>
<th>Pigeons and Doves</th>
<th>PRICE</th>
</tr>
<?php foreach($final_array as $row) {
?>
<tr>
<td><?php echo $row[‘bird_name’]; ?> </td>
<!– CURRENCY –> <td><?php if( get_field(‘currency’) && $row[‘price’] && is_numeric($row[‘bird_price’])): the_field(‘currency’); endif; ?>
<?php echo $row[‘price’]; ?>
</td>
</tr>
<?php } ?>
</table>
</div> <!– end pigeons loop –>
<?php endif; ?>
<!– ********************************** end pigeons and doves –>
They sort in their own original rows but the combined array is not merging.
sort()
sorts an array by value, but in this case the value is an array with two keys. So, which one should it pick for sorting, “bird_name” or “price”? The code cannot know. You have to tell it.
For this purpose usort() allows you to define a function that is used for sorting:
usort(
$final_array ,
function( $a , $b ) { return strcmp( $a['bird_name'] , $b['bird_name'] ); }
);
Thanks, but that is still sorting the list into the first and second acf rows.
Sorry, I will try to illustrate below:
pigeons_and_doves is an acf repeater that collects one set of data with two keys
other_pigeons_and_doves is an acf repeater that collects one set of data with two keys
when combining them into the array with this sort method it is listing them as
pigeons_and_doves key values first
A some name in list (pigeons_and_doves)
Z some name in list (pigeons_and_doves)
X some name in list (pigeons_and_doves)
other_pigeons_and_doves key values first
F some name in list (other_pigeons_and_doves)
B some name in list (other_pigeons_and_doves)
D some name in list (other_pigeons_and_doves)
so when they are run out in the loop that you advised with the usort they are displaying like this: (pigeons_and_doves all at top of list and other_pigeons_and_doves all at bottom of the list)
A some name in list (pigeons_and_doves)
X some name in list (pigeons_and_doves)
Z some name in list (pigeons_and_doves)
B some name in list (other_pigeons_and_doves)
D some name in list (other_pigeons_and_doves)
F some name in list (other_pigeons_and_doves)
INSTEAD OF LIKE THIS
A some name in list (pigeons_and_doves)
B some name in list (other_pigeons_and_doves)
D some name in list (other_pigeons_and_doves)
F some name in list (other_pigeons_and_doves)
X some name in list (pigeons_and_doves)
Z some name in list (pigeons_and_doves)
Thanks for that explanation.
On my end, this usort()
call does exactly what it is supposed to:
<?php
$arr = [
0 => [ 'bird_name' => 'C' , ] ,
1 => [ 'bird_name' => 'X' , ] ,
2 => [ 'bird_name' => 'A' , ] ,
3 => [ 'bird_name' => 'F' , ] ,
];
usort(
$arr ,
function( $a , $b ) { return strcmp( $a['bird_name'] , $b['bird_name'] ); }
);
var_dump( $arr );
/*
Prints:
array(4) {
[0]=>
array(1) {
["bird_name"]=>
string(1) "A"
}
[1]=>
array(1) {
["bird_name"]=>
string(1) "C"
}
[2]=>
array(1) {
["bird_name"]=>
string(1) "F"
}
[3]=>
array(1) {
["bird_name"]=>
string(1) "X"
}
}
*/
Are the names perhaps prefixed with information that does not belong there, for example:
and so on? No other possibility comes to my mind.
Please post an example of data that is not sorted correctly.
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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.