我需要PHP代码将数据保存到JSON文件。
需要的结果: " calendar":[{ " Pariharam":" pariharam&#34 ;, " Palamozhi":" palamozhi&#34 ;, "八字":[ { " HoroscopeID":" daily_horoscope_id&#34 ;, " Rasiname":"拉西&#34 ;, " SHORTDESCRIPTION":" shot_desc&#34 ;, " LongDescription":" long_desc" }, } ]
我的代码:
$sql=mysql_query("select * from daily_calendar_tbl");
$response = array();
$posts = array();
//$result=mysql_query($sql);
while($row=mysql_fetch_array($sql))
{
$caldate=$row['cal_date'];
$tamilyear=$row['tamil_year'];
$tamilmonth=$row['tamil_month'];
$tamildate=$row['tamil_date'];
$gtms=$row['gt_mrg_str'];
$gtme=$row['gt_mrg_end'];
$gtes=$row['gt_eve_str'];
$gtee=$row['gt_eve_end'];
$ggtms=$row['gow_mrg_str'];
$ggtme=$row['gow_mrg_end'];
$ggtes=$row['gow_eve_str'];
$ggtee=$row['gow_eve_end'];
$rst=$row['ragu_str'];
$ret=$row['ragu_end'];
$gst=$row['guli_str'];
$get=$row['guli_end'];
$yst=$row['yema_str'];
$yet=$row['yema_end'];
$sut=$row['sur_udha'];
$suras=$row['sur_astha'];
$yogam=$row['yogam'];
$chantrs=$row['chandrashtama'];
$soolam=$row['soolam'];
$phariharam=$row['pariharam'];
$palamozhi=$row['palamozhi'];
$selrasi=mysql_query("select * from daily_horoscope_tbl where horoscope_date='$caldate'");
while($rasidata=mysql_fetch_array($selrasi)){
$horoid=$rasidata["daily_horoscope_id"];
$rasiname=$rasidata["rasi"];
$shortdesc=$rasidata["shot_desc "];
$long_desc=$rasidata["long_desc"];
$horoscopedata[]=array('HoroscopeID'=> $horoid, 'Rasiname'=> $rasiname, 'ShortDescription'=>$shortdesc,'LongDescription'=> $long_desc);
}
$horoscopesres=$horoscopedata;
$posts[] = array('CalendarDate'=> $caldate, 'TamilYear'=> $tamilyear, 'TamilMonth'=>$tamilmonth,'TamilDate'=> $tamildate, 'GoodTimeMorningStart'=> $gtms,
'GoodTimeMorningEnd'=>$gtme,'GoodTimeEveningStart'=> $gtes, 'GoodTimeEveningEnd'=> $gtee, 'GowriGoodTimeMorningStart'=>$ggtms,'GowriGoodTimeMorningEnd'=> $ggtme, 'GowriGoodTimeEveningStart'=> $ggtes,
'GowriGoodTimeEveningEnd'=>$ggtee,'RahuStartTime'=> $rst, 'RahuEndTime'=> $ret, 'GulikaiStartTime'=>$gst,'GulikaiEndTime'=> $get, 'YamagandamStartTime'=> $yst,
'YamagandamEndTime'=>$yet,'SuryaUdhayamTime'=> $sut, 'SuryaAsthamanam'=> $suras, 'Yogam'=>$yogam,'Chandrashtama'=> $chantrs, 'Soolam'=> $soolam, 'Pariharam'=>$phariharam,'Palamozhi'=>$palamozhi,'horoscopes'=>$horoscopesres);
}
$response['calendardata'] = $posts;
$fp = fopen('articles.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
答案 0 :(得分:0)
你的json甚至不是一个有效的json试试这个
{
"calendar": {
"Pariharam": "pariharam",
"Palamozhi": "palamozhi",
"horoscopes": [{
"HoroscopeID": "daily_horoscope_id",
"Rasiname": "Rasi",
"ShortDescription": "shot_desc",
"LongDescription": "long_desc"
}]
}
}
其次,如果你想要占星术是一个数组,你想要传递数组
{
"calendar": {
"Pariharam": "pariharam",
"Palamozhi": "palamozhi",
"horoscopes": [{
"HoroscopeID": "daily_horoscope_id",
"Rasiname": "Rasi",
"ShortDescription": "shot_desc",
"LongDescription": "long_desc"
}, {
"HoroscopeID": "daily_horoscope_id",
"Rasiname": "Another",
"ShortDescription": "shot_desc",
"LongDescription": "long_desc"
}]
}
}
答案 1 :(得分:0)
这样的东西对你有用.. 你可能不得不为其他动态元素做foreach循环..你可以从这里得到一个想法。如果您发布其他代码,我可以更新我的答案。
$Pariharam = "pariharam";
$Palamozhi="palamozhi";
$horoscope= array('HoroscopeID'=>'daily_horoscope_id','Rasiname'=>'Rasi','ShortDescription'=>'shot_desc','LongDescription'=>'long_desc');
$calender=array();
$calender['calender']['Pariharam']=$Pariharam;
$calender['calender']['Palamozhi']=$Pariharam;
$calender['calender']['horoscope']=$horoscope;
print_r(json_encode($calender));exit;
<强>输出强>
{
"calender":{
"Pariharam":"pariharam",
"Palamozhi":"pariharam",
"horoscope":{
"HoroscopeID":"daily_horoscope_id",
"Rasiname":"Rasi",
"ShortDescription":"shot_desc",
"LongDescription":"long_desc"
}
}
}