PHP - 从分隔字符串制作多维关联数组 [英] PHP - Make multi-dimensional associative array from a delimited string

查看:41
本文介绍了PHP - 从分隔字符串制作多维关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能把这个字符串转过来吗:

Can you turn this string:

"package.deal.category"

变成这样的数组:

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆