生成阵列的JSON数组

时间:2015-08-12 12:17:11

标签: php arrays json

我想生成一个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) ?>

但我无法想象如何让数组获得正确的格式,我该怎么做才能解决它? 感谢

0 个答案:

没有答案