我有一个像这样的数组
'sections' => array(
array(
'title' => 'License',
'id' => 'license'
),
array(
'title' => 'General',
'id' => 'general'
),
array(
'title' => 'Header',
'id' => 'header'
),
array(
'title' => 'Footer',
'id' => 'footer'
),
array(
'title' => 'Slider',
'id' => 'slider'
),
)
如果许可证无效,我只想显示许可证选项卡。所以我想在数组中使用if else语句,比如这个
'sections' => array(
array(
'title' => 'License',
'id' => 'license'
),
if ($license_valid) {
array(
'title' => 'General',
'id' => 'general'
),
array(
'title' => 'Header',
'id' => 'header'
),
array(
'title' => 'Footer',
'id' => 'footer'
),
array(
'title' => 'Slider',
'id' => 'slider'
),
}
)
有人可以告诉我使用它的正确方法吗?
答案 0 :(得分:1)
$array = array(...);
if ($license_valid) {
$additional = array(
array(
'title' => 'General',
'id' => 'general'
),
array(
'title' => 'Header',
'id' => 'header'
),
array(
'title' => 'Footer',
'id' => 'footer'
),
array(
'title' => 'Slider',
'id' => 'slider'
),
);
$array = array_merge($array, $additional)
}
答案 1 :(得分:0)
嗯,array_merge很好,但是这里有一个不同的方法:
$arr = array('sections' => array(
array(
'title' => 'License',
'id' => 'license'
)
);
if ($license_valid) {
$arr['sections'][] = array(
'title' => 'General',
'id' => 'general'
);
$arr['sections'][] = array(
'title' => 'Header',
'id' => 'header'
);
$arr['sections'][] = array(
'title' => 'Footer',
'id' => 'footer'
);
$arr['sections'][] = array(
'title' => 'Slider',
'id' => 'slider'
);
}