I need to append the new line character in jsonencode .
My string :-
$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',}
But I need output like this :
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'
}
],
PHP got a neat little function called json_encode
. Now, you don't want to add your current string to the json_encode
function, but rather a object or array instead. Why? Well, cause you really don't want to manually put json strings together like that.
Instead of creating a string in the for-each loop, all you have to do is add the properties to an array
and then pass it to the json_encode
function and use the JSON_PRETTY_PRINT
flag.
Example:
$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);
You can use in double quotes in PHP:
$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 .= "},
" ;
}
<?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);
?>
Just build a new array with your desired structure and json_encode it. There's a very nice flag for encoding JSON_PRETTY_PRINT
which does exactly what you want. If you run it in console you will see the beautified output. If you run it in browser - click view source.
Output in the view source tab:
[
{
"title": "dfgdf",
"start": "324235",
"description": "gfdgdfg",
"url": "gfdgdfg"
},
{
"title": "dfgdf2",
"start": "324235_2",
"description": "gfdgdfg2",
"url": "gfdgdfg2"
}
]
For your $event_details use the following 3 lines after all the json data get populated
$openBracket = str_replace("{","{
",$event_details);
$comma = str_replace(",",",
",$openBracket);
$closeBracket = str_replace(",",",
",$comma);
After this just print the $closeBracket variable just like the below
echo $closeBracket;
Let me know if it is working
you do not need to append any new line to items
just enclose it in pre
tags
echo "<pre>";
//if printing php array
print_r($events);
//if printing json object
//print_r(json_decode($events));
echo "</pre>";