我需要在jsonencode中追加新行字符。如何在php json響應中追加新行字符
我的字符串: -
$event_details ='';
foreach($res as $k=>$val){
$event_details .= "{title:'".$val['event_name']."',";
$event_details .= "start:'".$val['start_date']."',";
$event_details .= "description:'".$val['event_detail']."'," ;
$event_details .= "url:'".$val['event_detail']."'," ;
$event_details .= "}," ;
}
Now I got output Like is
{title:'venuer request event 29-08-2015',start:'2015-09-02',description:'venuer request event
29-08-2015',url:'venuer request event 29-08-2015',},{title:'test event2',start:'2015-08-31',description:'test events1',url:'test events1', },{title:'venuer request event2',start:'2015-08-31',description:'venuer request event2',url:'venuer request event2',},{title
:'singer request event2',start:'2015-08-31',description:'singer request event2',url:'singer
request event2',}
但我需要的輸出是這樣的:
events: [
{
title: 'Event1',
start: '2015-08-04',
description :'sample',
url :'http://localhost.com/melodic_svn/singer/soniya-42',
},
{
title: 'Event2',
start: '2015-08-25',
description :'sample1',
url :'http://localhost.com/melodic_svn/singer/soniya-42'
}
],
你爲什麼不使用json_encode?您可以使用JSON_PRETTY_PRINT標記int以實現您的輸出 –
這不是您手動生成的JSON,而是JSOL。 (但是你可以自己添加'\ n')。但是,只要任何變量包含引號,您的簡單化方法就會失敗;沒有逃跑等。 – mario