我有一个非常基本的列表查询,我想在一个数组中显示所有列表
$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo"<pre>";
print_r($row);
echo "</pre>";
}
}
我的结果为
Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
Array
(
[id] => 2
[email] =>
[password] =>
[status] =>
)
Array
(
[id] => 3
[email] =>
[password] =>
[status] =>
)
但是我希望得到的结果看起来像这样。
Array
(
[0] => Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
[1] => Array
(
[id] => 1
[email] =>
[password] =>
[status] =>
)
)
任何人都可以告诉我如何获得这个结果
答案 0 :(得分:0)
您只需将行插入父数组,以获得所需的输出。通过在开始循环结果之前声明一个空数组,然后将每一行推入该父数组来执行此操作。见下文。
$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
$res = array(); // parent array here
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$array[] = $row; // push row into parent array
}
}
echo "<pre>";
print_r($res);
echo "</pre>";
答案 1 :(得分:0)
首先,您需要将所有行存储到attaylist中,然后打印
$sql = "SELECT * FROM vendor";
$result = mysqli_query($conn, $sql);
$resulyArray = array();
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
array_push($resulyArray , $row);
}
}
echo"<pre>";
print_r($resulyArray);
echo "</pre>";