用输出编码()
很难理解(json_encode)我使用的代码:
<?php
$query = mysql_query("SELECT * FROM messages ORDER BY ID");
while($fetch = mysql_fetch_assoc($query))
{
$titel = $fetch[title];
$post = array('items' => array( 0 => array('title' => "$title", 'description' => "$title")));
echo json_encode($post);
}
?>
输出:
{"items":[{"title":"title","description":"title"}]}
{"items":[{"title":"title","description":"title"}]}
但我想要一个输出:
{
"items": [
{
"title":"title",
"description":"title"
},
{
"title":"title",
"description":"title"
},
{
"title":"title",
"description":"title"
}
]
}
有人可以帮助我获得像上面的代码一样的输出吗?
试试这个:
<?php
$query = mysql_query("SELECT * FROM messages ORDER BY ID");
$post = array();
while($fetch = mysql_fetch_assoc($query))
{
$titel = $fetch[title];
$post['items'][] = array('title' => "$title", 'description' => "$title");
}
echo json_encode($post);
?>
编辑:更正
在循环之前将项目创建为数组,在循环内部追加,然后将其放入$ post并在循环之后进行编码。
链接地址: http://www.djcxy.com/p/47279.html上一篇: encode() with output