I have a table on the frontend, where the user can choose what types of columns he wants.
After submitting the form I get the array with selected columns.
For instance the user select following columns:
$columns = ["campaign_start_time", "campiagn_end_time", "campaign_id", "campaign_budget", "adset_name", "ad_name"]
Now I have to make the request to the facebook api, but facebook api only request query in form:
$query = "campaign{start_time, end_time, id, budget}, adset{name}, ad{name}"
Here is my question, what is the best way to convert $columns to $query in PHP language?
2
Answers
This solution splits apart the values based on underscores and then joins them back together as nested values based on the category/key. Note that this doesn’t check for items that don’t have a "prefix".
If you can construct your submitted data to be in this form:
Maybe using inputs or other constructs such as:
Then looping and building the query with the keys and values will do it:
Otherwise you’ll need to parse it to get what you need first, then execute the loop above using
$result
instead: