我试图过滤将在数组内部的值。为此我使用了以下语法:
$selected = array(
if ($request->annual!=0) {
'annual' => $request->annual,
}
if ($request->registration!=0) {
'registration' => $request->registration,
}
if ($request->monthly!=0) {
'monthly' => $request->registration,
}
if ($request->exam!=0) {
'exam' => $request->exam,
}
if ($request->laboratory!=0) {
'laboratory' => $request->laboratory,
}
if ($request->computer_lab!=0) {
'computer_lab' => $request->computer_lab,
}
);
但这会引发语法错误。
syntax error, unexpected 'if' (T_IF), expecting ')'
这是什么问题?任何人都可以帮助我吗?
答案 0 :(得分:0)
PHP不允许直接在Array体中使用IF。
按以下方式分配数组值:
$selected = array();
if ($request->annual != 0) {
$selected['annual'] = $request->annual;
}
if ($request->registration != 0) {
$selected['registration'] = $request->registration;
}
if ($request->monthly != 0) {
$selected['monthly'] = $request->monthly;
}
if ($request->annual != 0) {
$selected['annual'] = $request->annual;
}