我有一个数组,如下所示:
$widgets = array(
'ka' => array(
'name' => 'Kool-Aid',
'active' => true,
'priority' => 10,
'primacy' => 30,
'controller' => 'KoolAid'.$widgets['ka']['settings']['ka_type'].'Widget',
'settings' => array(
'ka_type' => 'BBQ',
),
),
);
如果您在'controller'行中注意到,我想将$widgets['ka']['settings']['ka_type']的值放入该值中。
我是否可以引用当前正在构建的数组中的键的值?
在值存在之前不能引用它。为临时变量赋值,然后在两个位置都使用它。
$kaType = 'BBQ';
$widgets = array(
'ka' => array(
'name' => 'Kool-Aid',
'active' => true,
'priority' => 10,
'primacy' => 30,
'controller' => 'KoolAid'.$kaType.'Widget',
'settings' => array(
'ka_type' => $kaType,
),
),
);
否,但您可以在控制器之前分配设置=>ka_type=>BBQ?
如果你不想先把值保存到变量中,那么你可以这样做。
在两个步骤上保存数组。
$widgets = array(
'ka' => array(
'name' => 'Kool-Aid',
'active' => true,
'priority' => 10,
'primacy' => 30,
'controller' => '',
'settings' => array(
'ka_type' => 'BBQ',
),
),
);
$widgets['ka']['controller'] = $widgets['ka']['settings']['ka_type'].'Widget';
或者,如果不仅仅有Ka,你可以像一样循环它
$widgets = array(
'ka' => array(
'name' => 'Kool-Aid',
'active' => true,
'priority' => 10,
'primacy' => 30,
'controller' => '',
'settings' => array(
'ka_type' => 'BBQ',
),
),
);
Foreach($widgets as $name=>$val){
$widget[$wid]['controller'] = $val['settings']['ka_type'].'Widget';
}
希望它能帮助