Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created May 6, 2020 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgyface/009543f97fea12b426b22cbabfd89d61 to your computer and use it in GitHub Desktop.
Save morgyface/009543f97fea12b426b22cbabfd89d61 to your computer and use it in GitHub Desktop.
PHP | Comma seperated string into ACF compatible multidimensional array
<?php
$string = "Skateboarding, Surfing, Horseriding"; // The original comma seperated string
$columns = explode(", ", $string); // Turns the string into an array
$column_array = array(); // Sets up the outer array
foreach ($columns as $key => $value) {
// Loops through the array creating keys and values
$column_array[$key]['skill'] = $value;
// $column_array[$key]['level'] = '0';
}
// As a function
function string_to_repeater($string, $separator) {
$columns = explode($separator, $string); // Turns the string into an array
$column_array = array(); // Sets up the outer array
foreach ($columns as $key => $value) {
// Loops through the array creating keys and values
$column_array[$key]['skill'] = $value;
// $column_array[$key]['level'] = '0';
}
return $column_array;
}
@morgyface
Copy link
Author

String to ACF Repeater

I had a problem, an API delivered data as WordPress custom fields. Most of the fields were text only 👍, however in situations where there were multiple values, it would be delivered as a comma separated string 👎.

Following import using the API plugin, I wanted my client to be able to edit the data so I needed to set-up ACF fields in a way that made them compatible. That way once the data was in place the client could edit the post and the associated meta values.

The problem was; no ACF field type stores data as a comma separated string, so I needed to transform the strings into a compatible format.

The solution is above as both code and a function. It essentially turns a string list into a repeater. Notice the commented out line $column_array[$key]['level'] = '0'; which would, in effect, add an additional sub field called level with a value of 0. That way the user could add further data to the list.

This is really useful to me. Probably not to anyone else, if it is, add a comment, it'd be good to know it's helped others! 🤙

@ledovej
Copy link

ledovej commented Dec 15, 2022

Hello,

thank god for this post, it seems to be closest to what I am searching for. Do you think you could help me?

I have a similar problem, only few things are different.
We have an ACF field created, in which there are multiple comma separated numbers, API imported.

E.g.
'101,552,597'

The numbers are being imported in different order.

I need to asign a custom text value to each number, and then update the value of another ACF field with the combined comma seperated string values of those numbers. Meaning:

The first ACF field is called 'vybaveni' and is containing '101,552,597' for example.
The string values for those numbers are defined by me, for example:

101 = valueone
552 = valuetwo
597 = valuethree

Then I have the second ACF field that is called 'vybaveni_string'. I need to update value of this field based on the numbers in the first field.
That means that if the first field is containing '101,552,597', the value of the second field should be 'valueone,valuetwo,valuethree'

or

If the first field contains just '101,597' then the second field value should be 'valueone, valuethree'. And so on, there are a lot of possible combinations.

I am not that good with PHP and coding in general, so I can't seem to get the code right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment