将多维数组转换为json格式

时间:2017-05-17 02:18:39

标签: php arrays json

我刚开始学习JSON。我必须创建一个静态多维数组,后来需要转换为json编码。现在我对创建多个数组感到困惑。以下是我尝试过的代码。我应该在这个数组中有很多帖子和作者。但它只打印单个帖子和作者。我不确定我错在哪里。

develop

1 个答案:

答案 0 :(得分:1)

试试这个,你可以查看live demo

<?php

    $data['post']= array(
        'title' => 'This is title',
        'message' =>'This is message',
        'datetime' => 'This is date time',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne',
        'userType' => 'Registered User',
        'address' => 'New York',
        'profilePic' => 'Profile picture'
        );
    $datas[] = $data;
    $data['post']= array(
        'title' => 'This is title1',
        'message' =>'This is message1',
        'datetime' => 'This is date time1',
        'bannerImage' =>''
        );
    $data['author']= array(
        'authorName' => 'Jason Bourne1',
        'userType' => 'Registered User1',
        'address' => 'New York1',
        'profilePic' => 'Profile picture1'
        );
    $datas[] = $data;
    $json = json_encode(array('data'=>$datas));
    echo $json;