Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Make multi-dimensional associative array from a delimited string [duplicate]

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.

like image 948
JayNCoke Avatar asked Aug 15 '26 05:08

JayNCoke


1 Answers

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?

like image 199
Explosion Pills Avatar answered Aug 16 '26 18:08

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!