2015-08-31 60 views
0

我需要在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' 
     } 

    ], 
+1

你爲什麼不使用json_encode?您可以使用JSON_PRETTY_PRINT標記int以實現您的輸出 –

+0

這不是您手動生成的JSON,而是JSOL。 (但是你可以自己添加'\ n')。但是,只要任何變量包含引號,您的簡單化方法就會失敗;沒有逃跑等。 – mario

回答

0

PHP有一個整潔一點功能叫json_encode。現在,您不想將當前字符串添加到json_encode函數中,而是將其替換爲對象或數組。爲什麼?那麼,因爲你真的不想手動把JSON字符串放在一起。
而不是在for-each循環中創建一個字符串,您只需將屬性添加到array,然後將它傳遞給json_encode函數並使用JSON_PRETTY_PRINT標誌。

例子:

$event_details = array(); 
foreach($res as $event){ 
    $eventArray = [ 
    'title' => $event['event_name'], 
    'start' => $event['start_date'], 
    ... And so on ... 
    ]; 
    array_push($event_details, $eventArray); 
} 
$json = json_encode($event_details, JSON_PRETTY_PRINT); 
0

可以用PHP在雙引號使用\r\n

$event_details =''; 

    foreach($res as $k=>$val){ 
    $event_details .= "{\r\ntitle:'".$val['event_name']."',\r\n"; 
    $event_details .= "start:'".$val['start_date']."',\r\n"; 
    $event_details .= "description:'".$val['event_detail']."',\r\n" ; 
    $event_details .= "url:'".$val['event_detail']."',\r\n" ; 
    $event_details .= "},\r\n" ; 
    } 
0
<?php 

$res = [ 
    [ 
     "event_name" => "dfgdf", 
     "start_date" => "324235", 
     "event_details" => "gfdgdfg" 
    ], 
    [ 
     "event_name" => "dfgdf2", 
     "start_date" => "324235_2", 
     "event_details" => "gfdgdfg2" 
    ] 
]; 

$event_details = []; 
foreach ($res as $k => $v) { 
    $event_details[$k]['title'] = $v['event_name']; 
    $event_details[$k]['start'] = $v['start_date']; 
    $event_details[$k]['description'] = $v['event_details']; 
    $event_details[$k]['url'] = $v['event_details']; 
} 

echo json_encode($event_details, JSON_PRETTY_PRINT); 
?> 

只是建立一個新的陣列所需的結構和json_encode它。編碼JSON_PRETTY_PRINT有一個非常好的標誌,它正是你想要的。如果你在控制檯中運行它,你會看到美化的輸出。如果你在瀏覽器中運行它 - 點擊查看源代碼。

輸出在查看源代碼標籤:

[ 
    { 
     "title": "dfgdf", 
     "start": "324235", 
     "description": "gfdgdfg", 
     "url": "gfdgdfg" 
    }, 
    { 
     "title": "dfgdf2", 
     "start": "324235_2", 
     "description": "gfdgdfg2", 
     "url": "gfdgdfg2" 
    } 
] 
0

爲了您的$ event_details使用下面的3行中的所有JSON數據獲取填充

$openBracket = str_replace("{","{\n",$event_details); 
$comma = str_replace(",",",\n",$openBracket); 
$closeBracket = str_replace(",",",\n",$comma); 

後,在此之後只打印$ closeBracket變量只是像下面那樣

echo $closeBracket; 

讓我知道它是否工作

0

你不需要追加任何新線項目
只是括在pre標籤

echo "<pre>"; 
//if printing php array 
print_r($events); 

//if printing json object 
//print_r(json_decode($events)); 
echo "</pre>";