Can you turn this string:
"package.deal.category"
Into an array like this:
$array['package']['deal']['category']
The value inside the index at this point can be anything.
Can you turn this string:
"package.deal.category"
Into an array like this:
$array['package']['deal']['category']
The value inside the index at this point can be anything.
What have you tried? The absolute answer to this is very easy:
$keys = explode('.', $string);
$array = array();
$arr = &$array;
foreach ($keys as $key) {
$arr[$key] = array();
$arr = &$arr[$key];
}
unset($arr);
...but why would this be useful to you?
I know this was asked some time ago, but for anyone else looking for another possible answer that doesn't involve loops, try using JSON.
To make $array['key1']['key2'] = $value
$key = 'Key1.Key2';
$delimiter = '.';
$value = 'Can be a string or an array.';
$jsonkey = '{"'.str_replace($delimiter, '":{"', $key).'":';
$jsonend = str_repeat('}', substr_count($jsonkey, '{'));
$jsonvalue = json_encode($value);
$array = json_decode($jsonkey.$jsonvalue.$jsonend, true);
$text = 'package.deal.category';
// convert 'package.deal.category' into ['package', 'deal', 'category']
$parts = explode('.', $text);
// changes ['package', 'deal', 'category'] into ['category', 'deal', 'package']
$revparts = array_reverse($parts);
// start with an empty array.
// in practice this should probably
// be the "value" you wish to store.
$array = [];
// iteratively wrap the given array in a new array
// starting with category, then wrap that in deal
// then wrap that in package
foreach($revparts as $key) $array = [$key => $array];
print_r($array);