Skip to content

Instantly share code, notes, and snippets.

@morgyface
Created May 6, 2020 11:20
Show Gist options
  • Select an option

  • Save morgyface/009543f97fea12b426b22cbabfd89d61 to your computer and use it in GitHub Desktop.

Select an option

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;
}
@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