我有一个如下所示的数组
$db_resources = array('till' => array(
'left.btn' => 'Left button',
'left.text' => 'Left text',
'left.input.text' => 'Left input text',
'left.input.checkbox' => 'Left input checkbox'
));
我需要动态地将这个数组转换为以下形式。
'till' => array(
'left' => array(
'btn' => 'Left button',
'text' => 'Left text',
'input' => array(
'text' => 'Left input text',
'checkbox' => 'Left input checkbox'
)
)
)
我尝试了使用explode函数对键进行处理。如果所有的键都只有一个“.”,那么这个方法是有效的。但是键是动态的,所以请帮我实现动态转换数组。我尝试了以下代码:
$label_array = array();
foreach($db_resources as $keey => $db_resources2){
if (strpos($keey,'.') !== false) {
$array_key = explode('.',$keey);
$frst_key = array_shift($array_key);
if(count($array_key) > 1){
$label_array[$frst_key][implode('.',$array_key)] = $db_resources2;
//Need to change here
}else{
$label_array[$frst_key][implode('.',$array_key)] = $db_resources2;
}
}
}
for_each
和$array_key
,因为explode返回一个数组。 - Sayed