简体   繁体   English

如何在mysqli提取循环中从多个数组创建数组

[英]How to create an array from multiple arrays in a mysqli fetch loop

I am trying to create an array so i can print it in json format. 我正在尝试创建一个数组,以便我可以json格式打印它。 I am querying a database using mysqli so and in the fetch loop sticking the returned information in an array, and trying to stick all of the arrays returned into one array and print that array in json format, but i cant seem to do it. 我正在使用mysqli查询数据库,并且在fetch循环中将返回的信息粘贴到数组中,并尝试将所有返回的数组粘贴到一个数组中,并以json格式打印该数组,但是我似乎无法做到这一点。 here is my code, any help or insight would be vastly appreciated. 这是我的代码,任何帮助或见解将不胜感激。

while($stmt->fetch())
{
$array .= ("title"=>$title,"date"=>$date,"body"=>$body)
}

print_r(json_encode($array));

but what is printed is 但是打印的是

ArrayArrayArrayArrayArray

can anyone please help? 谁能帮忙吗?

Use the [] syntax to append a new element onto your array $array 使用[]语法将新元素附加到数组$array

$array = array();
while($stmt->fetch())
{
  $array[] = array("title"=>$title, "date"=>$date, "body"=>$body);
}
print_r(json_encode($array));

Edited . 编辑 Originally missed that this used MySQLi bound variables, rather than a fetched row. 最初错过了它使用MySQLi绑定变量,而不是获取的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM