2014-03-07 34 views
-1

我正在讀取包含數據庫連接信息的.ini文件。 但是,當我嘗試訪問信息時,每當我嘗試訪問它時,都會收到未定義的偏移量和未定義的索引錯誤。代碼如下:未知的無法訪問陣列的元素

$connectInfo = parse_ini_file('/configuration.ini'); 
echo $connectInfo['hostname']; 
echo $connectInfo[0]; 
echo $connectInfo[0]['hostname']; 
echo $connectInfo[0][0]; 
var_dump($connectInfo); 

$connectInfo = parse_ini_file('/configuration.ini', TRUE); 
echo $connectInfo['hostname']; 
echo $connectInfo[0]; 
echo $connectInfo[0]['hostname']; 
echo $connectInfo[0][0]; 
var_dump($connectInfo); 

然而,當我做了vardump,我獲得以下的輸出:

array (size=6) 
    ''hostname'' => string 'localhost' (length=9) 
    ''database'' => string 'nestedtree' (length=10) 
    ''username'' => string 'root' (length=4) 
    ''password'' => string '' (length=0) 
    ''port'' => string '3306' (length=4) 
    ''socket'' => string '' (length=0) 

array (size=1) 
    'connection' => 
    array (size=6) 
     ''hostname'' => string 'localhost' (length=9) 
     ''database'' => string 'nestedtree' (length=10) 
     ''username'' => string 'root' (length=4) 
     ''password'' => string '' (length=0) 
     ''port'' => string '3306' (length=4) 
    ''socket'' => string '' (length=0) 

任何確定什麼這個問題,將不勝感激幫助。

+1

在.ini文件中是否使用了節​​標題中的引號? –

回答

0

您嘗試訪問的數組是「連接」數組的子數組。嘗試像這樣訪問它:

echo $connectInfo['connection']['hostname']; 
0

沒有出現它是代碼問題。似乎是Wamp服務器的問題。在Apache服務器上使用相同的代碼,代碼工作得很好。 感謝所有回答。