动态添加到数组

时间:2012-12-13 07:47:30

标签: php mysql arrays

//getting values from dataBase
$sql = "SELECT  `email` ,  `password` FROM  `user_inf`";
// Execute query
$result = mysql_query($sql,$con);
$email;
$pass;

while($row = mysql_fetch_array($result))
{
$email = $row['email'];
$pass  = $row['password'];
}



// create a dummy array
$doc = array (
1 => array ("Email", "Password")
     array ($email, $pass),
);

我需要动态地填充那个数组“doc”。我从数据库获取的值并使用while循环获取。我想将所有这些值放在该数组中

1 个答案:

答案 0 :(得分:2)

就这样做:

$result = mysql_query( "SELECT  `email` ,  `password` FROM  `user_inf`", $con );
$doc = array( array( "Email", "Password" ) );
while( $row = mysql_fetch_array( $result ) ) {
    $doc[] = array( $row['email'], $row['password'] );
}