Home › Forums › General Issues › Comma-separated array values › Reply To: Comma-separated array values
Can you provide a sample of the get_field('fieldname');
data? Here’s a quick debug snippet to see what the field holds:
echo '<pre>'.print_r(get_field('fieldname'), true).'</pre>';
If you already have a comma-separated value and want to convert it to an array, try this little snippet out:
$values = explode(',', get_field('fieldname'));
if( count($values)){
foreach($values as $value){
echo trim($value);
}
}
This snippet should work with a text string that looks something like one,two, three four, five-, six
and should output this: onetwothreefourfivesix
.
However, I have a feeling that you already have an array as provided by get_field('fieldname')
, so if that’s the case then you don’t need to explode at all, just loop with a foreach()
and output with something like echo $value.', ';
. A snippet like this might look like:
$values = get_field('fieldname');
if( count($values)){
foreach($values as $k=>$value){
if($k) echo ', ';
echo $value;
}
}
In this example, $k
is a counter that lets us know what item number we are currently working with. During the first loop, $k
will be 0
so no comma will be printed.
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.