2016-07-06 23 views
0

我想过滤将在数组内的值。为此,我使用以下语法:阵列中出现意外的'if'。 Laravel

$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

PHP不允许直接在数组体中使用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; 
    }