我想生成一个json数组,如下所示:
[
{
"title":"title of the post",
"id":"12",
"date_created":"2015-08-10",
"author":{
"name":"Name of the author",
"id":"12"
},
"location":{
"name":"name of the location",
"country":"country",
"id":12
}
}
]
我从数据库中获取数据如下
$stmt = $mysqli->query("SELECT * FROM posts");
$arr = array();
while ($post = $stmt->fetch_assoc()) { ?>
<?php
$post_author = get_author($mysqli, $post['author']);
$post_location = get_locaation($mysqli, $post['place']);
?>
<?php $arr[]['post'] = $post; ?>
//Add author and location to the array
<?php echo json_encode($arr) ?>
但我无法想象如何让数组获得正确的格式,我该怎么做才能解决它? 感谢