2015-03-31 57 views
-5

我有這種類型的陣列:語法錯誤PHP與陣列

Array(
    [0] => Array(
     [Success] => The file was uploaded. 
    )[1] => Array(
     [Error] => The file doesn 't exist. 
    )[2] => Array(
     [Success] => The file is supported. 
    ) 
) 

在我的代碼我使用,使這個帶ErrorSuccess消息顯示一個表格。

foreach($results as $innerArr) { 
    if(array_keys($innerArr)[0]=='Error') { $css = 'style="background:red"'; } 
    if(array_keys($innerArr)[0]=='Success') { $css = 'style="background:green"'; } 

    echo '<tr>'; 
    echo '<td '.$css.'></td>'; 
    echo '<td>'.array_values($innerArr)[0].'</td>'; 
    echo '</tr>'; 
} 

我的問題是: 我得到的所有此類錯誤的時間:

[Tue Mar 31 09:44:48 2015] [error] [client 172.***.140.***] PHP Parse error: syntax error, unexpected '[' in upload.php on line 229, referer: http://****.com/verification/index.html 

正如我有PHP 5.2,我怎麼能解決我的問題嗎?

謝謝。

+2

什麼是229線? – 2015-03-31 16:30:59

+0

Ligne 229是'array_keys($ innerArr)[0]'。 – francofolie 2015-03-31 16:43:45

回答

2

升級到PHP 5.4

或者,你必須與臨時變量耍着。函數返回值解引用僅在5.4中添加

2

這裏不起作用。當您使用array_values函數時,如果您使用PHP 5.2,則必須將它們保存在新變量中。

$values = array_values($innerArr); 
echo '<td>'.$values[0].'</td>'; 

您可以做的是將您的PHP版本更新到PHP 5.4+或更高版本。

+0

好吧,但行看起來這個:'array_keys($ innerArr)[0]'。 – francofolie 2015-03-31 16:44:19