2016-07-06 20 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; 
    }