我是PHP的新手。我有一个有三个用户的虚拟数据库,我想将他们的键和值插入一个数组。
$result_set=$database->GeneralQuery("SELECT * FROM users");
$the_object_array=array();
while ($row=mysqli_fetch_assoc($result_set)) {
foreach($row as $key => $value){
$the_object_array[$key] = $value;
}
}
print_r($the_object_array);
返回: -
Array ( [id] => 3 [username] => anne [password] => 123 [first_name] => Anne [last_name] => Baird ).....
换句话说,只有数据库表中的最后一个用户带有键和值。
如何让print_r($the_object_array)
使用键和值打印出所有三个用户
答案 0 :(得分:1)
尝试以下代码
$result_set=$database->GeneralQuery("SELECT * FROM users");
$the_object_array=array();
while ($row = mysqli_fetch_assoc($result_set)) {
$the_object_array[] = $row;
}
print_r($the_object_array);
答案 1 :(得分:0)
尝试这样的事情......
您已删除foreach中的密钥...就像这样$the_object_array[] = $value1;
$the_object_array=array();
$row = array(
'1'=>array(
'id' =>'3',
'name'=>'test'
),
'2'=>array(
'id' =>'3' ,
'name'=>'test1'
)
);
foreach($row as $key => $value){
foreach ($value as $key1 => $value1) {
$the_object_array[] = $value1;
}
}
print_r($the_object_array);