Everyone,
I have just started using ACF and mostly using feature to show data uploaded from csv file. Everything is working fine.
But in some fields i have data in the form of comma separated values such as Fiery Red, Leafy Green, Sky Blue.
My current output works as follows.
Available Colors - Fiery Red, Leafy Green, Sky Blue
I want to achieve;
Available Color - Fiery Red Available Colors - Fiery Red, Leafy Green
Thank you !
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use the explode() function to split a string into the array of values
So you have a string of colors, separated with a comma, where the comma is a delimiter:
$colors = 'Fiery Red, Leafy Green, Sky Blue';
Now use the explode() function (here is a link to the documentation) to convert it to the array:
$colors_array = explode( ', ', $colors );
As a result, you will get:
array(3)
(
[0] => string(9) "Fiery Red"
[1] => string(11) "Leafy Green"
[2] => string(8) "Sky Blue"
)
Now, when you have an array of values, you can display only the first value:
echo $colors_array[0];
or the first two elements:
$output_array = array_slice( $colors_array, 0, 2 ); echo implode( ', ', $output_array );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0