将多维数组转换为json格式 [英] convert multidimensional array to json format
本文介绍了将多维数组转换为json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚开始学习JSON.我必须创建一个静态多维数组,以后需要将其转换为json编码.现在,我对创建多个数组感到困惑.下面是我尝试过的代码.我应该在这个数组中有很多帖子和作者.但是它仅打印单个帖子和作者.我不确定我到底在哪里犯错.
I just started learning about JSON. I have to create a static multidimensional array which later required to convert to json encode. Now I get confused in creating multiple array. The below are the code that I have tried. I should have many post and author in this array. But it is printing only single post and author. I am not sure where I exactly did mistake.
<?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'
);
$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 = array($data);
$rss = (object) array('data'=>$datas);
$json = json_encode($rss);
echo $json;
推荐答案
尝试一下,您可以查看实时演示
try this, you can check the 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;
这篇关于将多维数组转换为json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文