2017-03-23 27 views
1

我創建了一個腳本,通過SQL數據庫中的while循環在現有數組中設置數組。在外部使用時設置數組,同時使用

while ($nextday = odbc_fetch_array($nextdayinfo)){ 
$username = $nextday['user_sign']; 
    if(isset($username)){ 
    $nextday[] = array('value' => $username, 'text' => $username); 
    } 
} 

這是代碼。如果我在IF條款後嘗試print_r($nextday),它會向我顯示所有信息,只要我在while子句後面加上print_r($nextday),它就會停止工作。

+0

什麼是'$ nextday'?陣列?或從'_fetch'返回的結果? –

+0

$ nextday是一個空數組。我想在這段時間創造它。 – Wvs

+1

但您的數組與數據庫行具有相同的名稱,因此每次迭代都會覆蓋它。 – Jerodev

回答

2

您對獲取的數據庫行使用與您的數組相同的變量。所以數組在每次迭代時被新行覆蓋。

嘗試在循環外部定義數組並使用其他名稱。

$array = []; 
while ($nextday = odbc_fetch_array($nextdayinfo)) { 
    $username = $nextday['user_sign']; 
    if (isset($username)) { 
     $array[] = [ 
      'value' => $username, 
      'text' => $username 
     ]; 
    } 
} 

print_r($array); 
相關問題