Here’s what I am currently seeing as a result of my code:
<?php
if( get_field('degrees') ) {
echo '<strong>Degrees: </strong>';
while ( have_rows('degrees') ) : the_row();
$array[] = get_sub_field('degree_select');
endwhile;
$foo = implode(', ', $array);
echo $foo;
}
?>
The sub_field “degree_select” is a selection field that has Labels and Values. I only want to call the Labels in a comma separated list.
Typically I would write something like:
<?php while (have_rows('degrees') ): the_row();
$value = get_sub_field('degree_select');
echo $value['label'];
endwhile; ?>
.. which works fine, but I haven’t been able to get it working using implode for this comma separated list yet. Any help would be greatly appreciated.
Thank you.
I figured out the solution. I was to use array_column which helped me focus on that single column of information.
<?php if( get_field('degrees') ) {
while ( have_rows('degrees') ) : the_row();
$array[] = get_sub_field('degree_select');
endwhile;
$foo = implode(', ', array_column($array, 'label'));
echo $foo;
}
?>
You can learn more about those here:
https://www.php.net/manual/en/function.array-column.php