admin管理员组

文章数量:1320609

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 !

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 !

Share Improve this question edited Oct 3, 2020 at 23:04 seezar asked Oct 3, 2020 at 8:57 seezarseezar 53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: advanced custom fieldsACFPick first or second value from comma separated values