将点分隔的键路径字符串和值声明转换为多维关联数组
我正在尝试创建一个数组,同时解析用点分隔的字符串
$string = "foo.bar.baz";
$value = 5
,以
$arr['foo']['bar']['baz'] = 5;
解析键,
$keys = explode(".",$string);
我怎样才能做到这一点?
I'm trying to create an array while parsing a string separated with dots
$string = "foo.bar.baz";
$value = 5
to
$arr['foo']['bar']['baz'] = 5;
I parsed the keys with
$keys = explode(".",$string);
How could I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以执行以下操作:
DEMO
如果传递字符串,您可以轻松制作一个函数以及作为参数的值并返回数组。
You can do:
DEMO
You can easily make a function out if this, passing the string and the value as parameter and returning the array.
您可以尝试以下解决方案:
You can try following solution:
这在某种程度上与您可以在此处找到答案的问题相关:
PHP 每个循环在数组中更深一层
您只需稍微更改一下代码:
This is somehow related to the question you can find an answer to, here:
PHP One level deeper in array each loop made
You would just have to change the code a little bit: