【发布时间】:2013-04-29 15:26:05
【问题描述】:
我想像这样剖析一个数组:
[
"ID",
"UUID",
"pushNotifications.sent",
"campaigns.boundDate",
"campaigns.endDate",
"campaigns.pushMessages.sentDate",
"pushNotifications.tapped"
]
转成这样的格式:
{
"ID" : 1,
"UUID" : 1,
"pushNotifications" :
{
"sent" : 1,
"tapped" : 1
},
"campaigns" :
{
"boundDate" : 1,
"endDate" : 1,
"pushMessages" :
{
"endDate" : 1
}
}
}
如果我能以类似键路径的方式在关联数组上设置一个值,那就太好了:
//To achieve this:
$dissected['campaigns']['pushMessages']['sentDate'] = 1;
//By something like this:
$keypath = 'campaigns.pushMessages.sentDate';
$dissected{$keypath} = 1;
如何在 PHP 中做到这一点?
【问题讨论】:
-
最上面的代码是字符串吗?
-
是的。字符串数组(实际上是键路径数组)。
-
拥有像 setValueForKeyPath($array, 1, 'campaigns.pushMessages.sentDate'); 之类的函数;
标签: php key associative-array