PHP - 从分隔的字符串进行多维关联数组 [英] PHP - Make multi-dimensional associative array from a delimited string
本文介绍了PHP - 从分隔的字符串进行多维关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以把这个字符串:
"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?
这篇关于PHP - 从分隔的字符串进行多维关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文