在外面使用时设置数组

时间:2017-03-23 12:49:58

标签: php arrays while-loop

我创建了一个脚本,我通过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),它会向我显示所有信息,只要我将print_r($nextday)放在while子句之后,它就会停止工作。

1 个答案:

答案 0 :(得分:2)

您对获取的数据库行使用与数组相同的变量。因此,每次迭代时,新行都会覆盖该数组。

尝试在循环外定义数组并使用不同的名称。

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

print_r($array);
相关问题